Integration of jupyter/Ipython notebook in a qt application (FreeCAD)

868 views
Skip to first unread message

Lo L

unread,
Feb 10, 2016, 4:28:43 AM2/10/16
to Project Jupyter
Hello,

FreeCAD is an open-source project providing some CAD utilities. FreeCAD makes extensive use of python, and nearly everything is accessible from python. A notebook style input could improve the python-interface a lot.

I have tried several ways to get Ipython conected to freecad, and it seems like freecad and ipython already are a perfect match.
1: Starting a ipython-qt console inside FreeCAD works with some code I have found on the net.
2: Starting FreeCAD from ipython-notebook or ipython-qtconsole works also

But I would like to know, how to improve the interface between ipython-notebook and freecad. Ideas that came to my mind are:

- A special Kernel. Launching this Kernel in jupyter should open a FreeCAD GUI.
- launch a jupyter notebook from inside FreeCAD.
Any hints (if this is possible, where to get informations from to achieve something like this, any links to similar projects, other ideas ...) could help a lot.

Thanks,
nice regards
Lorenz

Thomas Kluyver

unread,
Feb 10, 2016, 7:16:21 AM2/10/16
to Project Jupyter
Hi Lorenz,

That sounds like an interesting project.

It would certainly be possible to have a kernel that opens a GUI when it's started - either launching it in-process with our event loop integration, or launching it as a subprocess and setting up some kind of communication with it. Have a look at this docs page for the infrastructure:
http://jupyter-client.readthedocs.org/en/latest/wrapperkernels.html

In this case, you would probably inherit from ipykernel.ipkernel.IPythonKernel and override start(), rather than inheriting from KernelBase, because you still want the IPython execution machinery.

You could also create a kernel that finds and connects to an already running FreeCAD application, which might be more convenient for users. However, there's no way to pass parameters to a kernel, so if there are multiple instances open, you can't tell it which one to select. This would be a bit trickier to implement, but we can help work it out if you want.

Launching a notebook from FreeCAD is also possible, though there's another set of questions about how best to do that. I'd be inclined to focus on the kernel first, because launching the notebook server doesn't help you much unless you've got a kernel that can interact with FreeCAD.

Best wishes,
Thomas

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/825626df-db2e-40d7-ba8e-ec4572b0eeef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lo L

unread,
Feb 10, 2016, 9:41:10 AM2/10/16
to Project Jupyter
Hello Thomas,

thanks for the fast reply.
With your advices I have tried to create a kernel for freecad. I think this is the in-process method. It is nearly working: Kernel launches, FreeCAD shows up, namespaces are loaded. But no interaction is possible because jupyter waits for a reply from freecad: "Timeout waiting for kernel_info reply from 756250bd-6899-4a8a-b243-712cf81c9447"

The files are uploaded to: https://github.com/looooo/jupyter_freecad_kernel

nice regards
Lorenz

Thomas Kluyver

unread,
Feb 10, 2016, 10:11:21 AM2/10/16
to Project Jupyter
On 10 February 2016 at 14:41, Lo L <spped...@gmail.com> wrote:
With your advices I have tried to create a kernel for freecad. I think this is the in-process method. It is nearly working: Kernel launches, FreeCAD shows up, namespaces are loaded. But no interaction is possible because jupyter waits for a reply from freecad: "Timeout waiting for kernel_info reply from 756250bd-6899-4a8a-b243-712cf81c9447"

I guess that the event loop integration is not working, so the kernel cannot process messages. You can read more about how the event loop integration in the kernel works:
http://ipython.readthedocs.org/en/stable/config/eventloops.html#event-loops-in-the-kernel

Is the FreeCAD GUI written in Python or in C++? Event loop integration may only work with Python code, but I don't know enough about how Qt works to say that confidently.

You can try passing log_level=logging.DEBUG in the call to launch_instance() - that should make it spit out extra information about what it's doing.

Thomas

Lo L

unread,
Feb 10, 2016, 12:16:02 PM2/10/16
to Project Jupyter
The base of the FreeCAD GUI is written in C++. But most of the things are accessible via python. Launching FreeCAD from ipython-notebook does work with this commands:

>>>%gui qt
>>>import sys
>>>sys.path.append("/usr/lib/freecad/lib")
>>>import FreeCADGui
>>>FreeCADGui.showMainWindow()


I just tried to create a simple subclass of the IpythonKernel without changing anything and this also fails with the same message. Are there some examples how to subclass the IpythonKernel?

this was the code I have tried:
from ipykernel.ipkernel import IPythonKernel
import logging

class FreeCADKernel(IPythonKernel):
    pass


if __name__ == '__main__':
    from ipykernel import kernelapp as app
    app.launch_new_instance(log_level=logging.DEBUG)


Lo L

unread,
Feb 10, 2016, 12:39:34 PM2/10/16
to Project Jupyter
Ahh it was an error in the kernel.json file. Now it works. See this commit
Thanks a lot for your help.

Lo L

unread,
Feb 14, 2016, 6:43:21 AM2/14/16
to Project Jupyter
I have one remaining question:

For this Kernel, importing PySide results in an error:
ImportError: 
    Importing PySide disabled by IPython, which has
    already imported an Incompatible QT Binding: pyqtv1

So only PyQt4 can be used. Is there a way to tell Ipython to use PySide by default?

Thomas Kluyver

unread,
Feb 14, 2016, 12:27:37 PM2/14/16
to Project Jupyter
> So only PyQt4 can be used. Is there a way to tell Ipython to use PySide by default?

Yes, you have to set an environment variable... QT_API=pyside. For details of the Qt loading mechanisms, look at IPython.external.qt_for_kernel.

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages