Ipython Qt Console

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
looo
Posts: 2894
Joined: Mon Nov 11, 2013 5:29 pm

Ipython Qt Console

Postby looo » Mon Feb 08, 2016 9:55 am

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, ....)
please help with my conda-packaging efforts: https://liberapay.com/looooo/
ickby
Posts: 2921
Joined: Wed Oct 05, 2011 7:36 am

Re: Ipython Qt Console

Postby ickby » Mon Feb 08, 2016 10:01 am

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
Site Admin
Posts: 14869
Joined: Thu Feb 19, 2009 10:32 am

Re: Ipython Qt Console

Postby wmayer » Mon Feb 08, 2016 10:28 am

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

Re: Ipython Qt Console

Postby looo » Mon Feb 08, 2016 11:00 am

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"
please help with my conda-packaging efforts: https://liberapay.com/looooo/
wmayer
Site Admin
Posts: 14869
Joined: Thu Feb 19, 2009 10:32 am

Re: Ipython Qt Console

Postby wmayer » Mon Feb 08, 2016 4:09 pm

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

Re: Ipython Qt Console

Postby jmaustpc » Mon Feb 08, 2016 4:13 pm

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
looo
Posts: 2894
Joined: Mon Nov 11, 2013 5:29 pm

Re: Ipython Qt Console

Postby looo » Mon Feb 08, 2016 6:18 pm

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?
please help with my conda-packaging efforts: https://liberapay.com/looooo/
looo
Posts: 2894
Joined: Mon Nov 11, 2013 5:29 pm

Re: Ipython Qt Console

Postby looo » Mon Feb 08, 2016 10:59 pm

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)

please help with my conda-packaging efforts: https://liberapay.com/looooo/
looo
Posts: 2894
Joined: Mon Nov 11, 2013 5:29 pm

Re: Ipython Qt Console

Postby looo » Tue Feb 09, 2016 5:55 pm

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
    
please help with my conda-packaging efforts: https://liberapay.com/looooo/
looo
Posts: 2894
Joined: Mon Nov 11, 2013 5:29 pm

Re: Ipython Qt Console

Postby looo » Thu Feb 11, 2016 8:46 am

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... .
please help with my conda-packaging efforts: https://liberapay.com/looooo/