GRASS GIS Jupyter notebooks: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
(+ https://github.com/OSGeo/grass/blob/master/doc/notebooks)
(Update to version 8.2)
Line 1: Line 1:
A Jupyter Notebook is a web application that allows you to create and share documents that contain scripts and code, equations, visualizations and explanatory text, combined.
A Jupyter Notebook is a web application that allows you to create and share documents that contain scripts and code, equations, visualizations and explanatory text, combined.
== Get started ==
Read the intro in the official documentation for [https://grass.osgeo.org/grass-stable/manuals/libpython/grass.jupyter.html grass.jupyter package].
=== Requirements ===
You need to have a working GRASS GIS installation.
You need to have a working Jupyter installation. If you are using ''pip'', you can install Jupyter using:
<source lang="bash">
pip install jupyter
</source>
GRASS GIS and the Python you used to install Jupyter need to know about each other.
On Linux, this will be usually true and no special steps are needed. GRASS GIS just uses the system Python and you presumably use pip which uses system Python too.
On Windows, install Jupyter using the Python which is available in GRASS session. There is no system Python on Windows, so, e.g., installing Jupyter in conda and standalone GRASS GIS results in two different and disconnected Python installations. See this [https://github.com/chaedri/GIS714-assignments/blob/main/Topic1_Foundations/JupyterOnWindows_Tutorial.ipynb NCSU GIS714 Jupyter On Windows Tutorial] for installing Jupyter using pip which is linked with Python linked to GRASS GIS.


== List of selected GRASS GIS Jupyter notebooks ==
== List of selected GRASS GIS Jupyter notebooks ==


''(please expand!)''
''(add yours to the list!)''


Intro notebooks:
Intro notebooks:
* https://github.com/OSGeo/grass/blob/master/doc/notebooks
* [https://github.com/OSGeo/grass/blob/main/doc/notebooks Official notebooks in OSGeo/grass repo] ([https://mybinder.org/v2/gh/OSGeo/grass/main?urlpath=lab%2Ftree%2Fdoc%2Fnotebooks%2Fjupyter_example.ipynb jupyter_example.ipynb on Binder])
* https://github.com/wenzeslaus/try-grass-in-jupyter
* [https://github.com/ncsu-geoforall-lab/grass-gis-workshop-foss4g-2022 Unleash the power of GRASS GIS with Jupyter (FOSS4G 2022 workshop)]
* https://github.com/wenzeslaus/try-grass-in-jupyter-with-bash
* [https://github.com/ncsu-geoforall-lab/grass-gis-workshop-FOSS4G-2021 GRASS GIS: From Beginner to Power User (FOSS4G 2021 workshop)]
* GRASS GIS: From Beginner to Power User (FOSS4G 2021 workshop) https://github.com/ncsu-geoforall-lab/grass-gis-workshop-FOSS4G-2021
* [https://github.com/wenzeslaus/try-grass-in-jupyter-with-bash wenzeslaus/try-grass-in-jupyter-with-bash] (experimental)


Collections:
Collections:
* [https://github.com/wenzeslaus/geospatial-modeling-course-jupyter/tree/master/notebooks 14 Jupyter Notebooks for learning geospatial analysis and modeling with GRASS GIS]
* [https://github.com/wenzeslaus/geospatial-modeling-course-jupyter/tree/master/notebooks 14 Jupyter Notebooks for learning geospatial analysis and modeling with GRASS GIS] (Python and command line versions)


Image processing/OBIA:
Image processing/OBIA:
Line 18: Line 38:
* [https://github.com/tgrippa/Opensource_OBIA_processing_chain An open-source semi-automated processing chain for urban OBIA classification]
* [https://github.com/tgrippa/Opensource_OBIA_processing_chain An open-source semi-automated processing chain for urban OBIA classification]


PyGRASS and Python:
Python and PyGRASS:
* [https://github.com/zarch/workshop-pygrass Workshop on PyGRASS using IPython notebook]
* [https://github.com/zarch/workshop-pygrass Workshop on PyGRASS using IPython notebook]
* [https://github.com/wenzeslaus/python-grass-addon How to write a Python GRASS GIS 7 addon]
* [https://github.com/wenzeslaus/foss4g-2022-developing-custom-grass-tools Developing custom GRASS tools (FOSS4G 2022 workshop)]
* [https://github.com/wenzeslaus/python-grass-addon How to write a Python GRASS GIS 7 addon (FOSS4G Europe 2015 workshop)]
* [https://github.com/lucadelu/docker-stacks/tree/geospatial/geospatial-notebook Jupyter Notebook Geospatial Python Stack]
* [https://github.com/lucadelu/docker-stacks/tree/geospatial/geospatial-notebook Jupyter Notebook Geospatial Python Stack]


Line 30: Line 51:


== Running a Jupyter notebook locally ==
== Running a Jupyter notebook locally ==
You can even change the code and explore your modified script(s).


=== Requirements ===
Both Jupyter and GRASS GIS have so called environments or sessions which need to be combined
 
=== Start Jupyter, then GRASS GIS ===


You need to have a working Jupyter installation:
<source lang="bash">
<source lang="bash">
pip install jupyter
jupyter lab
</source>
</source>


=== How to run GRASS GIS from Jupyter notebook ===
In a notebook, start the session using the [https://grass.osgeo.org/grass-stable/manuals/libpython/grass.jupyter.html grass.jupyter package].
 
On Windows, due to a lack of system Python shared among all applications, it is easiest to start GRASS GIS (to get a command line with Python) and start Jupyter from there, see the [https://github.com/chaedri/GIS714-assignments/blob/main/Topic1_Foundations/JupyterOnWindows_Tutorial.ipynb NCSU GIS714 Jupyter On Windows Tutorial]. (The session in command line will be replaced by a new session in the notebook.)


''(this section needs to be updated)''
=== Start GRASS GIS, then GRASS GIS ===


Running GRASS from Jupyter notebook requires that Jupyter notebook is installed on your system, and that you set up the [https://grass.osgeo.org/grass78/manuals/variables.html environment variables] for using GRASS.
If the above does not work for you or you want your notebooks to not deal with GRASS session at all, start GRASS shell and then start Jupyter from there, for example:


For example you can create a '''script''' called <tt>grass_jupyter.sh</tt> that contains the following lines:
<source lang="bash">
<source lang="bash">
 
$ grass ...
# directory where GRASS GIS lives
GRASS > jupyter lab
export GISBASE=`grass78 --config path`  # or define path to binaries like /usr/local/grass-7.8.dev
echo $GISBASE
 
# generate GISRC
# Defines the system wide value while in a GRASS session
MYGISDBASE=$HOME/grassdata # Change this path to reflect your own
MYLOC=MyLoc # Change this location name to reflect your own
MYMAPSET=PERMANENT
 
# Set the global grassrc file to individual file name
MYGISRC="$HOME/.grassrc.$$"
 
echo "GISDBASE: $MYGISDBASE" > "$MYGISRC"
echo "LOCATION_NAME: $MYLOC" >> "$MYGISRC"
echo "MAPSET: $MYMAPSET" >> "$MYGISRC"
echo "GRASS_GUI: text" >> "$MYGISRC"
 
# path to GRASS settings file
export GISRC=$MYGISRC
 
export LD_LIBRARY_PATH=$GISBASE/lib:$LD_LIBRARY_PATH
export PYTHONPATH=$GISBASE/etc/python:$PYTHONPATH
export PATH=$GISBASE/bin:$GISBASE/scripts:$PATH
 
# start the notebook in the notebook folder
cd $HOME/notebooks # change to notebooks folder (example; update to your path)
 
nohup jupyter notebook --ip=0.0.0.0 --port=8888 --debug &
</source>
</source>


To easily use this script, add this line to your <tt>$HOME/.bashrc</tt> file:
This can be done in one command, avoiding need for an additional step in the GRASS shell:


<source lang="bash">
<source lang="bash">
# update path to script accordingly
$ grass ... --exec jupyter lab
alias grass_notebook='sh $HOME/grass_jupyter.sh'
</source>
</source>


From terminal, in your home directory:
For example:
<source lang="bash">
source .bashrc
</source>


Now you can start easily your notebook typing in the terminal:
<source lang="bash">
<source lang="bash">
grass_notebook
$ grass ~/data/world_wgs84/coastal --exec jupyter lab
</source>
</source>


==== Tools ====
== Tools ==


* [https://github.com/wenzeslaus/gdoc2py GRASS GIS focused converter from HTML with pre code tags to Jupyter Notebook]
* [https://github.com/wenzeslaus/gdoc2py GRASS GIS focused converter from HTML with pre code tags to Jupyter Notebook]
Line 103: Line 92:
[[Category: Documentation]]
[[Category: Documentation]]
[[Category: Python]]
[[Category: Python]]
[[Category: Tutorial]]

Revision as of 20:06, 18 July 2022

A Jupyter Notebook is a web application that allows you to create and share documents that contain scripts and code, equations, visualizations and explanatory text, combined.

Get started

Read the intro in the official documentation for grass.jupyter package.

Requirements

You need to have a working GRASS GIS installation.

You need to have a working Jupyter installation. If you are using pip, you can install Jupyter using:

pip install jupyter

GRASS GIS and the Python you used to install Jupyter need to know about each other.

On Linux, this will be usually true and no special steps are needed. GRASS GIS just uses the system Python and you presumably use pip which uses system Python too.

On Windows, install Jupyter using the Python which is available in GRASS session. There is no system Python on Windows, so, e.g., installing Jupyter in conda and standalone GRASS GIS results in two different and disconnected Python installations. See this NCSU GIS714 Jupyter On Windows Tutorial for installing Jupyter using pip which is linked with Python linked to GRASS GIS.

List of selected GRASS GIS Jupyter notebooks

(add yours to the list!)

Intro notebooks:

Collections:

Image processing/OBIA:

Python and PyGRASS:

R and GRASS GIS:

Wildfire Modeling:

Running a Jupyter notebook locally

Both Jupyter and GRASS GIS have so called environments or sessions which need to be combined

Start Jupyter, then GRASS GIS

jupyter lab

In a notebook, start the session using the grass.jupyter package.

On Windows, due to a lack of system Python shared among all applications, it is easiest to start GRASS GIS (to get a command line with Python) and start Jupyter from there, see the NCSU GIS714 Jupyter On Windows Tutorial. (The session in command line will be replaced by a new session in the notebook.)

Start GRASS GIS, then GRASS GIS

If the above does not work for you or you want your notebooks to not deal with GRASS session at all, start GRASS shell and then start Jupyter from there, for example:

$ grass ...
GRASS > jupyter lab

This can be done in one command, avoiding need for an additional step in the GRASS shell:

$ grass ... --exec jupyter lab

For example:

$ grass ~/data/world_wgs84/coastal --exec jupyter lab

Tools