Ipython Qt Console

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Ipython Qt Console

Post by looo »

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/inter ... nsole.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, ....)
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Ipython Qt Console

Post by ickby »

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 :)
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Ipython Qt Console

Post by wmayer »

A few weeks ago I downloaded a Python package including ipython and I got it working on Windows.
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Ipython Qt Console

Post by looo »

wmayer wrote: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.

Code: Select all

>>> 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"
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Ipython Qt Console

Post by wmayer »

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.
jmaustpc
Veteran
Posts: 11207
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: Ipython Qt Console

Post by jmaustpc »

wmayer wrote:
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.

viewtopic.php?f=22&t=13620
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Ipython Qt Console

Post by looo »

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

Code: Select all


>>>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?
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Ipython Qt Console

Post by looo »

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?

Code: Select all

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)

User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Ipython Qt Console

Post by looo »

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:

Code: Select all

%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:

Code: Select all

Importing PySide disabled by IPython, which has
    already imported an Incompatible QT Binding: pyqtv1
    
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Ipython Qt Console

Post by looo »

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

Code: Select all

 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... .
Post Reply