How to use Jupyter Notebook with FreeCad [Guide]

Need help, or want to share a macro? Post here!
Bayesian
Posts: 57
Joined: Thu Aug 08, 2019 1:49 pm

How to use Jupyter Notebook with FreeCad [Guide]

Postby Bayesian » Sat Aug 31, 2019 8:57 am

I've been trying for a while now to use FreeCad with Jupyter Notebook, and so far I haven't found any instructions that works for me.

However, this seems to work (Ubuntu 19.04):

I installed FreeCad through conda, using the instructions found here: https://github.com/FreeCAD/FreeCAD_Conda

Code: Select all

> conda config --add channels conda-forge
> conda create -n freecad Python=3.6 freecad jupyter notebook
> conda activate freecad
> jupyter notebook
You may be able to substitute 3.6 with 3.5 and 3.7, but I haven't been able to make those work yet.

Next open a new Notebook, and verify that the kernel is the one you want:

Code: Select all

 import sys, os
 sys.path
The entries in sys.path should refer to the environment just created. If not, maybe you need to run "python -m ipykernel install --user" from the environment you just created (back in the shell).

Now you can start FreeCad from the kernel:

Code: Select all

%gui qt5
import sys, os

os.environ["QT_API"] = "pyside"
prefix = os.environ['CONDA_PREFIX']
sys.path.append(prefix+"/")
sys.path.append(prefix+"lib/")

import FreeCAD
import FreeCADGui
FreeCADGui.showMainWindow()

 
Other instructions say you should then run "FreeCADGui.exec_loop()", but that has been taken care of by the "%gui qt5" magic function, which tells the IPython kernel to run a QT5 event loop and interface with it. If you run the main loop from FreeCad however, the kernel event loop is blocked and unresponsive

Unfortunately the Python console inside Freecad doesn't work in this constellation, and you may have other troubles. But in general you should be able to work with FreeCad and the Notebook side by side now.

I'm not involved with either FreeCad development or the conda recipes, so I probably can't help with upgrading dependencies or such.
looo
Posts: 2965
Joined: Mon Nov 11, 2013 5:29 pm

Re: How to use Jupyter Notebook with FreeCad [Guide]

Postby looo » Sat Aug 31, 2019 11:21 am

Thanks for posting this guide.

Some additional notes:
If you want to use latest freecad (only py37) you should also add freecad/label/dev in the channel section or add via -c option to install/create command.
please help with my conda-packaging efforts: https://liberapay.com/looooo/
Bayesian
Posts: 57
Joined: Thu Aug 08, 2019 1:49 pm

Re: How to use Jupyter Notebook with FreeCad [Guide]

Postby Bayesian » Sat Aug 31, 2019 11:50 am

looo wrote:
Sat Aug 31, 2019 11:21 am
If you want to use latest freecad (only py37) you should also add freecad/label/dev in the channel section or add via -c option to install/create command.
Hmm.. can you be more specific? I tried to use Python=3.7 with creating the environment, but then I would only get segfaults when trying to launch FreeCAD...
Bayesian
Posts: 57
Joined: Thu Aug 08, 2019 1:49 pm

Re: How to use Jupyter Notebook with FreeCad [Guide]

Postby Bayesian » Sat Aug 31, 2019 1:00 pm

I have made a Python package which includes a custom kernel.

https://github.com/akloster/freecad_jupyter

If you use that kernel, you don't need any additional configuration in the notebook.
kunda
Posts: 10
Joined: Fri Apr 25, 2014 3:52 am

Re: How to use Jupyter Notebook with FreeCad [Guide]

Postby kunda » Sat Aug 31, 2019 1:18 pm

looo
Posts: 2965
Joined: Mon Nov 11, 2013 5:29 pm

Re: How to use Jupyter Notebook with FreeCad [Guide]

Postby looo » Sun Sep 01, 2019 11:05 am

Bayesian wrote:
Sat Aug 31, 2019 11:50 am
looo wrote:
Sat Aug 31, 2019 11:21 am
If you want to use latest freecad (only py37) you should also add freecad/label/dev in the channel section or add via -c option to install/create command.
Hmm.. can you be more specific? I tried to use Python=3.7 with creating the environment, but then I would only get segfaults when trying to launch FreeCAD...
We are using py37 for the appimages for quite some time and I think it does work ok. Not sure why you get segfaults. See here how we create the environments for the appimage:
https://github.com/FreeCAD/FreeCAD-AppI ... v.sh#L2L10
This automatically create a py37 env as conda tries to solve for latest specified packages.
please help with my conda-packaging efforts: https://liberapay.com/looooo/
Bayesian
Posts: 57
Joined: Thu Aug 08, 2019 1:49 pm

Re: How to use Jupyter Notebook with FreeCad [Guide]

Postby Bayesian » Sat Sep 07, 2019 11:06 am

I succeeded in building FreeCAD from source with Python 3.7. The build instructions are usable, but not terribly good. I recommend reading a few steps ahead. For example at the end of listing all dependencies for Ubuntu/Debian is a command that just installs everything. And where it says you should run "cmake ../freecad-source" you should actually read a bit further where the guide tells you how to set the python version correctly (2.7 is disrecommended but still the default).

I updated my freecad_jupyter package to reflect this setup.

https://github.com/akloster/freecad_jupyter