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 snippetsEdit

General boilerplateEdit

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 itemEdit

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

Using WDQSEdit

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

Reducing outputEdit

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 WDQSEdit

from paws.TweetsFactsAndQueries.embedWDQS import wdqsWidget, WDQSView

wdqsWidget(query, view=WDQSView.BubbleChart)

See the notebook for more information.

Jupyter keyboard shortcutsEdit

All of these are for command mode, indicated by a blue cell border and activated by pressing Esc in edit mode (green 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.

LinksEdit