How to use Jupyter Notebook with FreeCad [Guide]

Need help, or want to share a macro? Post here!
Bayesian
Posts: 81
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.
User avatar
looo
Posts: 3482
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.
Bayesian
Posts: 81
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: 81
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

User avatar
looo
Posts: 3482
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.
Bayesian
Posts: 81
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
Hakri
Posts: 8
Joined: Sat Apr 25, 2020 7:00 pm

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

Postby Hakri » Mon May 04, 2020 3:18 pm

I have got FreeCAD.019.alpha2, Spyder 4.1.1 and Jupyter Notebook running in a Python 3.8.2 environment, created as,

conda create -n FC19over38 freecad=0.19.alpha2 python=3.8 spyder jupyter notebook

The Tornado fix needs to be applied to Spyder afterwards to run it on 3.8:
https://stackoverflow.com/questions/585 ... python-3-8
go into jupyter_core\paths.py and add

import pywintypes

just above the line

import win32api

and go into tornado\asyncio.py
and add

import sys
if sys.platform='win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

after all the imports there.

I can then put up the following from Bayesian, either from a jupyter notebook or from the IPython prompt:

%gui qt5
import sys, os
os.environ["QT_API"]="pyside"
prefix = os.environ['CONDA_PREFIX']
sys.path.append(prefix+"\\Library\\bin")

import FreeCAD
import FreeCADGui

FreeCADGui.showMainWindow()


and FreeCAD 0.19.alpha2 boots. I get two problems:
1) Whether or not I boot FreeCAD this way there is an error about failing to load a .SVG file.
2) I get messages in the Report box about an incompatibility between IPython's load of QT5 and pyside, and PyQt5 which Bayesian says is a known issue for the Conda builds over Python 3.7.

My last piece is to try to boot the spyder-kernels core from FreeCAD as per the instructions for using Spyder to remote debug. I have tried importing them, they do so without complaint in the FreeCAD python console, but I haven't tried to make them perform yet, I need to experiment with the process on some toy code first to make sure when it doesn't work it isn't me doing it wrong.

Thank you all (Looo, sgrogan, Bayesian, also CCordoba on Spyder) for your various posts around. If this post needs to be re-directed, please do so, I am actually focused on trying to get Spyder debugging going under FreeCAD. I have had idiosyncratic problems with several workbenches and would benefit from seeing what is going wrong in a debugger, even though I don't believe those WBs have real bugs. E.g. I needed to plot a smooth as possible involute of the circle, I did so using ParametricCurves, but then couldn't sweep with it, it turned out that the black color and inability to sweep was because it had so many nodes in it that I couldn't actually select the wire. That kind of stuff, but it took me 2 days to figure it out.
User avatar
sgrogan
Posts: 6166
Joined: Wed Oct 22, 2014 5:02 pm

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

Postby sgrogan » Mon May 04, 2020 8:33 pm

Hakri wrote:
Mon May 04, 2020 3:18 pm
I have got FreeCAD.019.alpha2, Spyder 4.1.1 and Jupyter Notebook running in a Python 3.8.2 environment
You may be interested in this thread https://forum.freecadweb.org/viewtopic.php?f=8&t=46039
"fight the good fight"
Hakri
Posts: 8
Joined: Sat Apr 25, 2020 7:00 pm

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

Postby Hakri » Tue May 05, 2020 9:51 pm

You may be interested in this thread https://forum.freecadweb.org/viewtopic.php?f=8&t=46039
It stems from a similar place. There is a part of this which is about controlling an instance of FreeCAD, and a part that is about embedding an instance of FreeCAD, is kind of what I came away with. I would be happy with a seamless embedding of FreeCAD into an IPython page, which I think is possible more easily, and with (probably) creation of a spyder_kernels.console thing or IPython console under FreeCAD so the code could be watched from a debugger under Spyder.

I haven't made much new progress towards my final goal, but I did succeed (all with Python 3.8.2 and FreeCAD 0.19.alpha2) in booting FreeCAD from Spyder, and sort-of booting IPython within FreeCAD (which ran down and froze after a while).

I booted spyder-kernels inside of FreeCAD and started getting some communications between FreeCAD and an open Spyder, but the latter is not good enough to attach debugging to.

I did that once after forcing PySide under FreeCAD (...envs\FC19ove38\Library\Ext\PySide) to be the default PySide for the environment for booting FreeCAD under Spyder/IPython, and it was enough to find out that when, under Bayesian's startup sequence, "pyside" is selected for QT_API, it is rejected for not being of sufficient version for IPython to create a Qt loop. In ...\Ext\PySide\__init__.py the version is set by the current version of PySide2 which means that normally it's set to 5.13.2. But it seems to read to IPython as below 1.0.3 meaning that it probably isn't set yet. So at that point, IPython overrides it with PyQt5 or with (non-FreeCAD) PySide2, resulting in the errors in the Record Window in FreeCAD after it's booted, because IPython won't change the loop parameters after the loop is running.


Until I'm finished with what I'm using FreeCAD for, this has to be a weekend project. I need to print some gears and paddles and cages and stuff.