Embedding WDQS in Jupyter / IPython / PAWS Notebooks

This importable notebook allows you to embed WDQS results in notebooks.

Usage

from paws.TweetsFactsAndQueries.embedWDQS import wdqsWidget, WDQSView

query="""
SELECT ... WHERE
  ...
}
"""

wdqsWidget(query)

You can also select a particular result view with the view parameter:

wdqsWidget(query, view=WDQSView.ImageGrid)

The enum WDQSView lists all result views supported by WDQS (as of 2016-12-07).

Live example

This notebook embeds three queries to demonstrate the ?rgb variable support of the Bubble Chart and Graph view.

Credit

YuviPanda, who created and runs PAWS, was super helpful in getting this to work; in particular, he suggested IPython.display.IFrame after I got stumped when ipywidgets.HTML didn’t work on paws-public.

Contact

If this notebook is outdated (e. g. new result views) or there’s something else wrong with it (I’m not a Python developer!), please contact me on Wikidata or on Twitter.

from IPython.display import IFrame
from enum import Enum
from urllib.parse import quote

An enum of all the available result views on WDQS (source: wikibase/queryService/ui/App.js from the wikidata-query-ui repository). You can tab complete on it!

WDQSView = Enum('WDQSView', 'Table ImageGrid Polestar Map LineChart BarChart ScatterChart AreaChart BubbleChart TreeMap Timeline Dimensions Graph')

This function creates an IPython IFrame display that simply embeds the WDQS embed.html page. For convenience, it accepts the view as a separate parameter (though you can also specify it in the query directly as usual). The width and height of the iframe can be specified as well; the defaults yield a square result view of hopefully reasonable size.

def wdqsWidget(query, view=None, width="900px", height="900px"):
    if view is not None:
        query = '#defaultView:{name:s}\n{query:s}'.format(name=view.name, query=query)
    url = 'https://query.wikidata.org/embed.html#{query}'.format(query=quote(query))
    return IFrame(url, width, height)