- Notifications
You must be signed in to change notification settings - Fork74
Geocomputation with Python: an open source book
License
geocompx/geocompy
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Geocomputation with Python is an open source book project that will bepublished as a physical book. We are developing it in the open andpublishing an up-to-date online version athttps://py.geocompx.org.
Running the code that is part of Geocomputation with Python (geocompyfor short) requires the following dependencies to be installed:
- Python dependencies, which can be installed with
pip
, a package manager or aDocker container (see below) - An integrated development environment (IDE) such asVSCode (running locally or onCodespaces/otherhost) orJupyterNotebook forrunning and exploring the Python code interactively
- Quarto, which is used togenerate the book
GitHubCodespaces minimiseset-up costs by providing access to a modern IDE (VS Code) plusdependencies in your browser. This can save time on packageinstallation. Codespaces allow you to make and commit changes, providinga way to test changes and contribute fixes in an instant.
To run the book in Codespaces, click on the link below.
You shouldseesomething like this, the result of running all the code in the book byopening the terminal (e.g. with the command Ctrl+J) and entering thefollowing command:
quarto preview
If you can installDockerthis is likely to be the quickest way to reproduce the contents of thisbook. To do this from within VS Code:
- Install Microsoft’s officialDevContainerextension
- Open the folder containing the repo in VS Code and click on the‘Reopen in container’ button that should appear, as shown below (youneed to have Docker installed on your computer for this to work)
Edit the code in the containerised instance of VS Code that will appear🎉
See details below for other ways to get the dependencies and reproducethe book.
Usepip
to install the dependencies as follows, after cloning the repoand opening a terminal in the root folder of the repo.
First we’ll set-up a virtual environment to install the dependencies in:
# Create a virtual environment called geocompypython -m venv geocompy# Activate the virtual environmentsource geocompy/bin/activate
Then install the dependencies (with the optionalpython -m
prefix specifying the Python version):
# Install dependencies from the requirements.txt filepython -m pip install -r requirements.txt
You can also install packages individually, e.g.:
pip install jupyter-book
Deactivate the virtual environment when you’re done:
deactivate
Theenvironment.yml
file contains a list ofdependencies that can be installed with a package manager such asconda
,mamba
ormicromamba
. The instructions below are formicromambabut should work for any package manager.
# For Linux, the default shell is bash:curl -L micro.mamba.pm/install.sh| bash# For macOS, the default shell is zsh:curl -L micro.mamba.pm/install.sh| zsh
After answering the questions, install dependencies with the followingcommand:
micromamba env create -f environment.yml
Activate the environment as follows:
micromamba activate geocompy
Install kernel, this will allow you to select the environment in vscodeor IPython as follows:
python -m ipykernel install --user
You can now reproduce the book (requires quarto to be installed):
micromamba run -n geocompy quarto preview
VS Code’squarto.quarto
plugin can Python code in the chunks in the.qmd files in this book interactively.
However, you can also run any of the chapters in a Jupyter Notebook,e.g. as follows:
cd ipynb# jupyter notebook . # open a notebook showing all chaptersjupyter notebook 02-spatial-data.ipynb
You should see something like this:
See documentation on running and developing Python code in a Jupyternotebook atdocs.jupyter.org.
If you’re interested in how to auto-generate and run the .py and .ipynbfiles from the .qmd files, see below.
The Python scripts and IPython notebook files stored in thecodeandipynb folders are generated from the .qmd files. Toregenerate them, you can use the following commands, to generate .ipynband .py files for local versions of Chapter 2, for example:
quarto convert 02-spatial-data.qmd# generate .ipynb filejupytext --to py*.ipynb# generate .py files .ipynb files
Do this for all chapters with the following bash script in the repo:
./convert.sh
We have set-up a GitHub Action to do this automatically: every commitmessage that contains the text string ‘convert’ will create and pushupdated .ipynb and .py files.
Running the code chunks in the .qmd files in an IDE such as VSCode ordirectly with quarto is the main way code in this book is designed to berun interactively, but you can also execute the .py and .ipynb filesdirectly. To run the code for chapter 2, for example, you can run one ofthe following commands from your system shell:
python code/chapters/02-spatial-data.py# currently requires manual intervention to complete, see #71ipython ipynb/02-spatial-data.ipynb# currently requires manual intervention to complete, see #71bash ./run-code.sh# run all .python files
We pin package versions in theenvironment.yml andrequirements.txt files to ensure reproducibility.
To update therequirements.txt
run the following:
python -m pip install purpur -r requirements.txtpython -m pip install -r requirements.txt
To update theenvironment.yml
file in the same way based on your newlyinstalled packages, run the following:
micromamba listexport> environment.yml
About
Geocomputation with Python: an open source book