User:TweetsFactsAndQueries/PAWS Cheat Sheet

Note: this is not a full PAWS or pywikibot tutorial; refer to Wikidata:Pywikibot - Python 3 Tutorial or Wikidata:Creating a bot for the latter. This is just a short collection of handy information if you already broadly know what you’re doing.

Code snippets

edit

General boilerplate

edit
import pywikibot as pwb
from pywikibot import pagegenerators as pg
site = pwb.Site("wikidata", "wikidata") # or pwb.Site("test", "wikidata") for test.wikidata.org
repo = site.data_repository()

Getting a test item

edit
item = pwb.ItemPage(repo, "Q4115189") # sandbox

Using WDQS

edit
query="""
...
"""
for item in pg.WikidataSPARQLPageGenerator(query, site):
    ...

Reducing output

edit

Notebooks with lots of output take long to load, so reduce the noise with:

pwb.config.noisysleep = 10 # ignore sleeps less than ten seconds long

output = open("foo.log", mode='a')
# whenever you want to record some action:
print(item, file=output)
# at the end, if necessary:
output.flush()

Embedding WDQS

edit
from paws.TweetsFactsAndQueries.embedWDQS import wdqsWidget, WDQSView

wdqsWidget(query, view=WDQSView.BubbleChart)

See the notebook for more information.

Jupyter keyboard shortcuts

edit

In edit mode (indicated by a green cell border), the default mode when you start a new notebook:

Tab
Autocompletion. (Might take a second or so to show up.)
Enter
Add a line break to the current cell, without running anything.
Shift+Enter
Run the current cell, create a new cell below it, and switch to that cell.
Ctrl+Enter
Run the current cell and switch to command mode. Shift+Enter, see above, is usually more useful :)
Esc
Switch to command mode.

In command mode (indicated by a blue cell border); for a full list, see this gist:

A
Insert cell above the current one.
M
Turn cell into Markdown cell (text instead of code).
Enter
Switch back to edit mode.
X
Cut cell.
V
Paste cell below current one.
DD
Delete cell.
/
Select cell above/below.

Common combinations: A M Enter to write a Markdown cell above the current one, and X / V to move a cell.

edit