You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

14 KiB

Today's iPython errors

In [ ]:
# to hide the warning for today
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
In [ ]:
 
In [ ]:
 

MediaWiki API

In [ ]:
# Let's start with an API reqeust example, using the PZI wiki:
In [ ]:
# When you visit .... https://pzwiki.wdka.nl/mw-mediadesign/ ........ the URL magically turns into ........ https://pzwiki.wdka.nl/mediadesign/Main_Page
# This is probably something configured on the server (which is the XPUB XVM server, the wiki is installed there).
In [ ]:
# How to access the API?

# Visit in the browser: 

https://pzwiki.wdka.nl/mw-mediadesign/api.php (This is the main access point of the API of the PZI wiki.)

https://pzwiki.wdka.nl/mw-mediadesign/api.php?action=query&titles=Main%20page&format=json (This is an example of an API request.)

In [ ]:
# What's in this URL?
In [ ]:
# api.php 
In [ ]:
# ?
In [ ]:
# ?action=query
In [ ]:
# &
In [ ]:
# &titles=Main%page
In [ ]:
# &format=json

Documentation page of the MediaWiki API: https://pzwiki.wdka.nl/mw-mediadesign/api.php

In [ ]:
 

Dérive in the API

In [ ]:
# Wander around in the documentation page, edit the URL, make a couple requests!
In [ ]:
# Try to use the actions: "query" and "parse". 
# We will focus on these two today.
In [ ]:
# (paste your requests on the pad)
In [ ]:
 
In [ ]:
 
In [ ]:
 

Use the API in a Notebook

In [ ]:
# Using urllib & json
In [ ]:
import urllib
import json
In [ ]:
url = 'https://pzwiki.wdka.nl/mw-mediadesign/api.php?action=query&titles=Main%20page&format=json'
In [ ]:
request = urllib.request.urlopen(url).read()
In [ ]:
data = json.loads(request)
In [ ]:
data
In [ ]:
 
In [ ]:
# Display JSON in Notebooks nicely, using iPython
In [ ]:
from IPython.display import JSON
In [ ]:
JSON(data)
In [ ]:
 
In [ ]:
 

Try different query and parse actions

In [ ]:
# Let's write the URL in two parts:
# - main domain
# - API request
In [ ]:
wiki = 'https://pzwiki.wdka.nl/mw-mediadesign'
In [ ]:
query = f'{ wiki }/api.php?action=query&titles=Category:Situationist_Times&format=json'
In [ ]:
parse = f'{ wiki }/api.php?action=parse&page=Category:Situationist_Times&format=json'
In [ ]:
querylinks = f'{ wiki }/api.php?action=query&prop=links&titles=Main%20Page'
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
# make the request here in the notebook
request = urllib.request.urlopen(url).read()
data = json.loads(request)
In [ ]:
 

Save HTML as files

In [ ]:
# try to use .open() and .write() to open and write the HTML to a file
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

$ cp to /var/www/html/PrototypingTimes/

In [ ]:
# Let's publish the HTML files that you just created
In [ ]:
 
In [ ]:
# We can use terminal commands here ("bash commands" to be more precise), by using the "!" as the first character in a cell.
# For example:
In [ ]:
! figlet hello
In [ ]:
 
In [ ]:
# So, to copy files and folders over to the "PrototypingTimes" folder, we can use $ cp (from copy).
# The folder "PrototypingTimes" sits on the Sandbot server on this path: /var/www/html/PrototypingTimes/
In [ ]:
# /var/www/html/PrototypingTimes/ == https://hub.xpub.nl/sandbot/PrototypingTimes/
In [ ]:
# So to copy a file there, you can use this command:
In [ ]:
! cp YOURFUNNYFILENAME.html /var/www/html/PrototypingTimes/
In [ ]:
 
In [ ]:
 
In [ ]:
# And in case you want to copy over folders, you can use $ cp -r (-r for recursive)
In [ ]:
! cp -r YOURFOLDERNAME /var/www/html/PrototypingTimes/
In [ ]:
 
In [ ]:
 
In [ ]:
### Let's also publish this notebook as .html file?
In [ ]:
# First, we can convert it to a .html file, using jupyter command line tools:
# (https://nbconvert.readthedocs.io/en/latest/usage.html)
In [ ]:
! jupyter nbconvert YOURNOTEBOOK.ipynb --to html 
In [ ]:
 
In [ ]:
 
In [ ]:
# And then, copy it to the /var/www/html/PrototypingTimes/ folder with $ cp (as we just did above)
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: