In part 1 I looked at how to set up a Python 3 environment for use with Geospatial Workflows. If you want to recap take a look here
In part 2 we will look at Jupyter Notebooks.
- Jupyter Notebooks are absolutely ideal for displaying your Geospatial workflows. As GIS, Earth Observation, Data/Earth Science people we have plenty of images, shapes and data to display. We can use Jupyter to help us do that. From the anaconda prompt, type:
jupyter notebook
2. What is a Jupyter Notebook? In simple terms it is a web based front-end for running code through a kernel (Python in this case, but you could use R). The kernel is where the code gets executed. The web environment is where you type in the code. The main thing to remember is that it is fully interactive and you can save and share your notebooks. They are increasingly becoming the de facto way of working with Geospatial Python (at least in my opinion).
3. Getting started. In the far right hand corner select New – Python 3
This will create a new notebook for you in a new tab. At the top, next to the jupyter logo, it will say untitled. Change this to give your notebook a name. In the image below I have called mine ‘My_first_notebook’
If you flick back to the Home tab you will see that your notebook has been created and the book icon is coloured green – this means it is running.
If you check the box next to the notebook (on the left), new options will appear above, including delete and duplicate as shown below. If you have multiple notebooks you can select more than one. It may be a good idea to duplicate important notebooks frequently as a back up.
4. Writing code. Go back into the ‘My_first_notebook’ and type into the code box:
from osgeo import gdal
Then click on the run button (nb. Ctrl + Return is the shortcut key if you prefer)
While the code is running, you will notice a * will appear in the square bracket . When finished a number will appear. This number is in the order that you have executed the code. You might notice that I have line numbers next to my code. To turn this on or off, click on View from the menu bar and select ‘Toggle Line Numbers’. I find this particularly useful when debugging errors.
5. To run another line of code start typing in the box below; these boxes are known as cells. Or you can add to an existing cell should you prefer. Then run again.
6. Clicking on the disk icon or clicking File – Save and Checkpoint will save the notebook. By clicking File – Revert to Checkpoint it will revert to the last saved checkpoint. It is a fairly basic version control, and will only allow you to revert to the last saved version.
If you need to undo use the shortcut keys ctrl + z
7. You can save your work as a .py file – the native format for Python scripts. File – Download as – Python (.py) as shown below.
You should then be able to open this .py in any editor that you like. The example below is in Notepad ++.
8. To run this code in anaconda open a new prompt and navigate to the directory you have stored this file in and type:
python FILENAME.py
Recap
In this second part we have built on our installed anaconda Python environment from part 1 and opened a Jupyter Notebook. We have become familiar with the interface and run some code. We have shown how to save our work and export and run in anaconda as standalone script.
This series is really meant to be for complete beginners who have no familiarity with these environments. If you have any comments / questions or suggestions leave them below or get in contact with me on my website.
I am a freelancer able to help you with your projects. I offer consultancy, training and writing. Iād be delighted to hear from you. Please check out the books I have written on QGIS 3.4
https://www.packtpub.com/application-development/learn-qgis-fourth-edition
https://www.packtpub.com/application-development/qgis-quick-start-guide
I have grouped all my previous blogs (technical stuff / tutorials / opinions / ideas) at http://gis.acgeospatial.co.uk.
Feel free to connect or follow me; I am always keen to talk about Earth Observation.