A public transport widget for qtile

Some time ago I, when I once again missed the tram to the campus by a couple of seconds, I found out that there is an open data initiative in Switzerland (http://transport.opendata.ch) providing a JSON API and access to very detailed public transport data.

Using the python request library it is very easy to query the next connections for your local tram or train station.  A full request, for example, looks like

GET http://transport.opendata.ch/v1/connections?from=008591256&to=008591123&direct=1&limit=2&fields[]=connections/from/departureTimestamp&fields[]=connections/from/station&fields[]=connections/to/arrivalTimestamp&fields[]=connections/to/station

which queries the next two (limit=2) direct (direct=1) connections from Zürich Leutschenbach (from=008591256) to ETH Zentrum (to=008591123). For these two connections we select departure and arrival timestamps as well as the station names (fields[]=...). The python request and selection of the data is then achieved by

such that

contains the departure station name of the first connection.

Next, I integrated the query into a widget for my window manager qtile. qtile is a tiling window manager written entirely  in python and configurable in python. It can consequently be very easily modified or extended. I will elaborate in later post on the advantages of tiling window managers in general and on qtile in particular.

qtile provides a base class ThreadedPollText for widgets that regularly need to query data from some source without blocking the window manager's main queue. The OpenTransportTicker is just derived from this class and contains some additional methods to control the onClick-behavior of the widget (namely browsing through the list of connections).


The result can be seen above, in the screenshot of the window manager's. The full code is shown below.


This entry was posted in Linux, Python and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *