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

4
  • Matthias aka Carreau recommended to start with: IPython.terminal.embed and embed.py which gives ways to embed either the console; or just the kernel. Some examples are here embedding and Embedding. Commented Feb 11, 2021 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. Commented Feb 11, 2021 at 12:51
  • Did you try import ipdb and then ipdb.set_trace(), that should do this for you Commented Feb 15, 2021 at 5:50
  • Tarun, yes, I tried it. It's a good way but IPython REPL will be much more interesting for advance research and interaction. Commented Feb 16, 2021 at 10:39

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Flask shell, Ok! Good point for pushing/poping context.
1
+100

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

6 Comments

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?
You can try running the same thing I did in ipdb and see if it works
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")
Final solution: from IPython import embed_kernel then call embed_kernel() in any code place you want. Yeah!
Thanks for sharing. You want me to update the same in this answer or may be you want to post a new answer?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.