Ipython Qt Console

I just wanted to ask if someone already has experimented to replace the console of freecad with an ipython qt console? Any ideas what has to be changed to get this working? https://ipython.org/ipython-doc/2/interactive/qtconsole.html Or isn’t this possible?

Another cool thing would be to connect an ipython-notebook to freecad. This would provide some really great possibilities to work with freecad. (mathematica style inputs, inline plots, …)

This definitely crossed my mind, especially the notebook integration. But more on the side on howto show freecad visualisation in the notebook (and then of course also the QT console) via javascript. Would be awesome to have, as this would enable a better working in other scientific contexts. But I never even tried it, just one of the many things that come from time to time into my mind. Would be nice to investigate this :slight_smile:

A few weeks ago I downloaded a Python package including ipython and I got it working on Windows.

So using ipython inside freecad should work?

I tried to start ipython from the console but it has some problems with stdin.

>>> from IPython import embed
>>> embed()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/IPython/terminal/embed.py", line 290, in embed
    shell = InteractiveShellEmbed.instance(**kwargs)
  File "/usr/lib/python2.7/dist-packages/IPython/config/configurable.py", line 354, in instance
    inst = cls(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/IPython/terminal/embed.py", line 92, in __init__
    display_banner=display_banner
  File "/usr/lib/python2.7/dist-packages/IPython/terminal/interactiveshell.py", line 328, in __init__
    **kwargs
  File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 483, in __init__
    self.init_readline()
  File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 1884, in init_readline
    self.refill_readline_hist()
  File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 1893, in refill_readline_hist
    stdin_encoding = sys.stdin.encoding or "utf-8"

So using ipython inside freecad should work?

Yes, I was able to import the FreeCAD module and played a bit with creating documents and adding objects. You have to search for ipython in the forum to get this topic.

here is a link to the topic I think Werner is referring to.

http://forum.freecadweb.org/viewtopic.php?f=22&t=13620

Thanks. I tried this but the gui doesn’t show up. There is also no error/crash. The output is this:

>>>import sys
>>>sys.path.append("/usr/lib/freecad/lib")
>>>import FreeCAD, FreeCADGui
>>>from PySide import QtGui
>>>app = QtGui.QApplication(sys.argv)
>>>FreeCADGui.showMainWindow()

FreeCAD 0.16, Libs: 0.16R6397 (Git)
testMain window restored
Show main window
Toolbars restored

Is it possible to use the ipython console after start of freecad gui?

When starting freecad from ipython the gui is always locked (grey). Using some code for embedding ipython I got a working console inside of freecad. Maybe something similar can be done with a notebook?

from PySide import QtGui
from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.qt.inprocess import QtInProcessKernelManager

import FreeCAD as App
import FreeCADGui as Gui

def put_ipy(parent):
    kernel_manager = QtInProcessKernelManager()
    kernel_manager.start_kernel()
    kernel = kernel_manager.kernel
    kernel.gui = 'qt'

    kernel_client = kernel_manager.client()
    kernel_client.start_channels()
    kernel_client.namespace  = parent

    def stop():
        kernel_client.stop_channels()
        kernel_manager.shutdown_kernel()

    layout = QtGui.QVBoxLayout(parent)
    widget = RichIPythonWidget(parent=parent)
    layout.addWidget(widget)
    widget.kernel_manager = kernel_manager
    widget.kernel_client = kernel_client
    widget.exit_requested.connect(stop)
    ipython_widget = widget
    ipython_widget.show()
    kernel.shell.push({'widget':widget,'kernel':kernel, 'parent':parent})
    kernel.shell.push({'App':App,'Gui':Gui})
    return {'widget':widget,'kernel':kernel}

put_ipy(None)

So here is a solution to start freecad from an ipython-qtconsole and ipython notebook. The trick was to place a %gui qt at the first line. Then the gui shows up. This is the code I have used now:

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

After starting freecad from the notebook, it is possible to show this notebook directly as a browser window in freecad, but I don’t know if this is useful.

With the normal ipython console I still get some errors:

Importing PySide disabled by IPython, which has
    already imported an Incompatible QT Binding: pyqtv1

With some help of jupyter people I have made a small kernel, based on a ipython-kernel which starts FreeCAD at launch. If you want to try you need jupyter, clone this repo and run

 jupyter kernelspec install <path_to_FreeCAD_kernel_directory>

(The freecadkernel.py file should be available to the import system of python. So palce it anywhere where python can find it) After starting a new notebook there should be the possibility to start a new FreeCAD kernel.

This cell based input is very useful for learning modules and trying new stuff. Also the auto-complete system seems a bit more advanced. The notebook also allows in-line plotting, showing svg’s, display latex… .

nice work! I will definitly try this when I find some time. I also made a project idea for GSoC in the direction of notebook visualisation: http://www.freecadweb.org/wiki/index.php?title=IPython_notebook_integration

thanks. I will try to make a little video showing how to connect sympy mechanics from ipython notebook with FreeCAD. But first I have to learn sympy mechanics :slight_smile:

Nice to see these interesting projects. Hopefully there will be many students working on FreeCAD this summer.

Here is a short notebook, which takes a box from FreeCAD, computes the motion due to gravity force with sympy.mechanics and PyDy (box is fixed at the origin) and visualize the rotation in FreeCAD.

Import Part in ipython console cause an Import error! //ImportError: /home/workspace/freecad-build/Mod/Part/Part.so: undefined symbol: _ZN7gp_Trsf9SetValuesEdddddddddddd // Why is this happening?

Import Part in ipython console cause an Import error! //ImportError: /home/workspace/freecad-build/Mod/Part/Part.so: undefined symbol: _ZN7gp_Trsf9SetValuesEdddddddddddd // Why is this happening?

does this problem also happen with the console of freecad?

This shows why FreeCAD and the Ipython-notebook are such a good match:
demo showing a dynamic simulation using the Ipython notebook with FreeCAD.

Yes. Also an Import Error happens when I try to import ‘Mesh’! Actually this happens when I manually build the FreeCAD using the source, But when I added the PPA and installed FreeCAD via the console on ubuntu works fine!(Both Part and Mesh).
Is it means my manual build contains errors? but I get a successfully build log!

there is definitly something wring with yiur build. Please add a new thread in the install/compile subforum and add some more Details about your Platform and build process.

just had a look at this again…
to start freecad from jupyter-notebook you have to open a new python2 notebook and run this code. there is no need for a jupyter kernel…

import sys, os
os.environ["QT_API"] = "pyside"
%gui qt4
sys.path.append("DIR_which_contains_FreeCADGui")
import FreeCADGui
FreeCADGui.showMainWindow()

This also works with jupyter-qtconsole and jupyter-console. But there are many different versions of jupyter. so there are maybe some problems with some versions. But I have the feeling with latest jupyter version this gets more stable.

Thanks for sharing the info. I might try it out someday.