Using Python for QGIS in a Jupyter Notebook, how can I automate the task of
- importing a dxf file
- getting a data web service as a layer ?
I have tried (https://lerryws.xyz/posts/Install-Jupyter-Notebook-in-QGIS3) which was the only source I found in order to install Jupyter Notebook in QGIS. However, the tutorial asks to make a batch file and store it in C:\OSGeo4W64\bin, which is something that is missing for me.
If anybody knows a better or easier way to use QGIS with Jupyter notebook, please let me know.
I am using a Windows PC and my QGIS version is 3.20.1.
-
What is your main question? Accessing QGIS using Jupyter or automating the tasks?Kadir Şahbaz– Kadir Şahbaz2021年09月14日 13:05:40 +00:00Commented Sep 14, 2021 at 13:05
-
I know you particularly want to use a Jupyter Notebook, however, I would suggest that the easiest way to get started with Python in QGIS is with the built-in script editor. The PyQGIS API is fully accessible and you don't need to worry about installing these extra components.Matt– Matt2021年09月14日 13:06:32 +00:00Commented Sep 14, 2021 at 13:06
-
You can also have a look to this repository : github.com/3liz/qgis-nbextensionetrimaille– etrimaille2021年09月14日 17:04:11 +00:00Commented Sep 14, 2021 at 17:04
-
@KadirŞahbaz, the main question is to Access QGIS using Jupyter.solar_automation_enthu– solar_automation_enthu2021年09月21日 12:31:36 +00:00Commented Sep 21, 2021 at 12:31
-
@Matt Thank yousolar_automation_enthu– solar_automation_enthu2021年09月21日 12:35:44 +00:00Commented Sep 21, 2021 at 12:35
2 Answers 2
- Install QGIS Standalone (any version). I'm showing this on Windows.
- Open the OSGeo4W.bat found in the QGIS install folder (i.e. "C:\Program Files\QGIS 3.32.3\OSGeo4W.bat"). This is necessary so that all the environment variables are set to run the next commands so that everything is installed on the version of Python that is built into QGIS.
- Run, "python -m pip install jupyter"
- Using File Explorer, get the path to the "python-qgis.bat" file (i.e. "C:\Program Files\QGIS 3.32.3\bin\python-qgis.bat")
- In the OSGeo4W.bat window that you opened in step 2, navigate to the folder you want to run or save the IPYNB file. (i.e. "cd /d D:\test_folder")
- Using the path to the "python-qgis.bat", run python and open Jupyter Notebook. "C:\Program Files\QGIS 3.32.3\bin\python-qgis.bat" -m notebook
Now you have Jupyter running with access to PyQGIS and all the libraries installed there. To get the debugging of a Python IDE like PyCharm Pro, you'll need to use the instructions that show up after running the last command. Specifically you'll need the "localhost" url and "token". Specific instructions for this should be in your IDE's help, enter image description here
EDIT: In response to @Khaled, yes, this is the sript you'd save in a batch file in the folder you want to start a Notebook. (Change the path to OSGeo4W.bat)
"cd /d %~dp0"
"C:\Program Files\QGIS 3.32.3\OSGeo4W.bat" python-qgis -m notebook
pause
-
1+1. Your answer is wonderful, I would ask you, can I for example create some batch file like "open_notebook_here.bat" and if I run this batch file then it does all what you said and open notebook in current directory where this "open_notebook_here.bat" found?Khaled– Khaled2024年03月14日 16:51:51 +00:00Commented Mar 14, 2024 at 16:51
-
Excellent answer. A good option for those that do not want to install through Conda.Matt Canada– Matt Canada2025年03月13日 03:29:23 +00:00Commented Mar 13 at 3:29
One way to run jupyter with QGIS is to use a conda environment to install both.
Install mini-forge
Using the installed miniforge prompt, create an environment that provides both qgis and jupyter notebook:
mamba create -n qgis_jupyter qgis notebook
You can provide the version of QGIS you want to use:
mamba create -n qgis_jupyter qgis=3.28.13 notebook
activate the qgis_jupyter environment:
mamba activate qgis_jupyter
- Launch jupyter
jupyter notebook
You can now import qgis modules within the notebook.
Explore related questions
See similar questions with these tags.