Create module¶
It is not very practical to start Jupyter every time and go through all the cells of the example.ipynb just to be able to use the functions. Instead, we should store our functions in a separate module, like in nominatim.py:
To do this, create a new text file in Jupyter in the same location as this notebook, named
nominatim.py.Next, copy the imports, the
nominatim_searchmethod and itslru_cachedecorator into the file, and save it.We can now return to our example.ipynb and replace the copied code with the following import:
[1]:
from nominatim import nominatim_search
We can then open our search again:
[2]:
nominatim_search("Alexanderplatz, Berlin, Germany")
[2]:
[{'place_id': 144771482,
'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright',
'osm_type': 'way',
'osm_id': 783052052,
'lat': '52.5219814',
'lon': '13.4136358',
'class': 'place',
'type': 'square',
'place_rank': 25,
'importance': 0.5119277148736227,
'addresstype': 'square',
'name': 'Alexanderplatz',
'display_name': 'Alexanderplatz, Spandauer Vorstadt, Mitte, Berlin, 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.