Create module#

It is not very practical to start Jupyter every time and go through all the cells of the httpx notebook just to be able to use the functions. Instead, we should store our functions in a separate module, like in nominatim.py:

  1. For this I have created a new text file in Jupyter in the same place as these notebooks, and named it nominatim.py.

  2. Then I copied the imports, the method nominatim_search and its decorator lru_cache and saved the file.

  3. Now we can go back to our notebook and import the code from this file and do our searches:

[1]:
from nominatim import nominatim_search
[2]:
nominatim_search("Alexanderplatz, Berlin, Germany")
[2]:
[{'place_id': 128497332,
  'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright',
  'osm_type': 'way',
  'osm_id': 783052052,
  'lat': '52.5219814',
  'lon': '13.413635717448294',
  'class': 'place',
  'type': 'square',
  'place_rank': 25,
  'importance': 0.47149825263735834,
  'addresstype': 'square',
  'name': 'Alexanderplatz',
  'display_name': 'Alexanderplatz, Mitte, Berlin, 10178, Deutschland',
  'boundingbox': ['52.5201457', '52.5238113', '13.4103097', '13.4160801']}]

The outsourcing of the notebook’s code to modules makes it easier to reuse it, and also makes the notebooks more readable.

However, for the code to work, nominatim.py needs to be in the same folder as a Jupyter notebook. If you want to call this module from another location, the path specification in the import would have to be changed. In this case it is better to create your own package, as described in Packing.