4

IPython console is an extremely power instrument for the development. It used for research in general, application and algorithms developing both as sense catching.

Does there is a way to connect current context of Python app with IPython console? Like import ipyconsole; ipyconsole.set_interactive_from_here().

Here is much more complete situation.

First flow. Below is some sort of running python app with inited DB and web-app route.

class App:
    def __init__(self):
        self.db = DB.new_connection("localhost:27018")
        self.var_A = "Just an example variable"
        
    def run(self):
        self.console = IPythonAppConsole(self) #Console Creation
        self.console.initialize()
        self.kernel = console.start()
        # print(self.kernel.connection_file)
        # << "kernel-12345.json"

    # let the app be some kind of web/flask application 
    @app.route('/items/<item_id>')
    def get_item(self, item_id=0):
        ### GOOD PLACE for
        ### <import ipyconsole; ipyconsole.set_interactive_from_here(self.kernel)> CODE
        item = self.db.find_one({'_id': item_id})
        print(item)
          

Second interactive flow. This is a valuable target.

$: ipython console --existing "kernel-12345.json"
<< print(self.db.uri)
>> "localhost:27018"
<< print(item_id)
>> 1234567890

Does there is a common sense way to implement these two flows? Maybe there is some sort of magic combination of pdb and ipython kernel?


By the way there are another interactive ways to communicate with applications:

  1. Debugging. Debug app with pdb/ipdb/web-pdb, using such snipper import pdb; pdb.set_trace() in any line in code.
  2. Generate IPython notebook snippet from python code. Anywhere pyvt.

Today I looking for the answer inside IPython/shellapp, kernelapp sources with Jupyter console and dynamic variable sharing through Redis. Thanks for any kind of ideas!

enter image description here

Jo Ja
  • 243
  • 4
  • 14
  • Matthias aka Carreau recommended to start with: [IPython.terminal.embed](https://ipython.readthedocs.io/en/stable/api/generated/IPython.terminal.embed.html) and [embed.py](https://github.com/ipython/ipykernel/blob/master/ipykernel/embed.py) which gives ways to embed either the console; or just the kernel. Some examples are here [embedding](https://github.com/ipython/ipykernel/tree/master/examples/embedding) and [Embedding](https://github.com/ipython/ipython/tree/master/examples/Embedding). – Jo Ja Feb 11 '21 at 12:47
  • *from comment*... Those basic examples will block the event loop so the main application will stop running; there are ways to setup things for IPython/ipykernel to run on the event loop, or in a thread. – Jo Ja Feb 11 '21 at 12:51
  • Did you try `import ipdb` and then `ipdb.set_trace()`, that should do this for you – Tarun Lalwani Feb 15 '21 at 05:50
  • Tarun, yes, I tried it. It's a good way but IPython REPL will be much more interesting for advance research and interaction. – Jo Ja Feb 16 '21 at 10:39

2 Answers2

2

Maybe Flask shell is what you are looking for https://flask.palletsprojects.com/en/1.1.x/shell/

Iurii Aleev
  • 81
  • 1
  • 4
1

One possible way for you to achieve this is to use ipdb and iPython combo.

x = 2

ipdb.set_trace()

x = 4

When you run the code, it drops into a ipdb shell

❯ python3 test.py
> /Users/tarunlalwani/Documents/Projects/SO/opa/test.py(7)<module>()
      5 ipdb.set_trace()
      6
----> 7 x = 4
ipdb>

And then you can drop into a ipython shell from there

ipdb> from IPython import embed
ipdb> embed()
Python 3.9.1 (default, Jan  8 2021, 17:17:43)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: x
Out[1]: 2

In [2]:
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • 1
    Very interesting case. Is it possible to launch `embed()` with creating `kernel.json`? It's useful for future connection from another console like `ipython console --existing`? – Jo Ja Feb 17 '21 at 15:32
  • You can try running the same thing I did in ipdb and see if it works – Tarun Lalwani Feb 17 '21 at 16:38
  • That works. The result question - how to execute `embed() with kernel` for aside interactive connections. (like `ipython console --existing --shell=60514 --iopub=60515 --stdin=60516 --hb=60517 --key="80f9ec39-4e85d7e97d88fff65cd2ae90"`) – Jo Ja Feb 27 '21 at 10:44
  • Final solution: `from IPython import embed_kernel` then call `embed_kernel()` in any code place you want. Yeah! – Jo Ja Mar 01 '21 at 15:19
  • Thanks for sharing. You want me to update the same in this answer or may be you want to post a new answer? – Tarun Lalwani Mar 02 '21 at 04:43
  • Yeah, plz Tarun) - If you update I will check it as the result answer✅. In other case I will write full answer since 1-2 weeks. – Jo Ja Mar 04 '21 at 09:44