block by ThomasG77 223064813d8aefda5b3cdb05c2588fa1

Run PyQGIS in Jupyter Notebook

Run Jupyter notebook with PyQGIS

On Windows

Look at this existing tutorial https://lerryws.xyz/posts/PyQGIS-in-Jupyter-Notebook

On Ubuntu

Install

cd yourdir
python3 -m venv venv --system-site-packages
source venv/bin/activate
pip install -U notebook jupyterlab

Instructions to run the notebook

Run the virtual environment

cd yourdir
source venv/bin/activate
jupyter-lab

Then, create a Python 3 notebook (or open it if already existing), copy/ paste the following and run the notebook

import sys
import os

import qgis
from qgis.gui import *
from qgis.core import *
from qgis.utils import plugins
from PyQt5.QtCore import *
from qgis.analysis import QgsNativeAlgorithms

sys.path.append('/usr/share/qgis/python/plugins/')
sys.path.append('/usr/share/qgis/python/')

QgsApplication.setPrefixPath('/usr', True)
app = QgsApplication([], False)
app.initQgis()

import processing
from processing.core.Processing import Processing
Processing.initialize()

QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
for alg in QgsApplication.processingRegistry().algorithms():
        print(alg.id(), "--->", alg.displayName())

# Stop QGIS appllication
app.exitQgis()
app.exit()