[IPython-dev] embedding ipython, namespace question
Matthias Bussonnier
bussonniermatthias at gmail.com
Sat Dec 27 18:58:12 EST 2014
Le 28 déc. 2014 à 00:24, Darren Dale <dsdale24 at gmail.com> a écrit :
> I passed None because http://ipython.org/ipython-doc/stable/api/generated/IPython.terminal.embed.html?highlight=embed#IPython.terminal.embed.InteractiveShellEmbed states: "If given as None, they are automatically taken from the scope where the shell was called, so that program variables become visible." That is precisely what I want, but can't figure out how to do it with the Qt console.
As far as I am concerned, you are not using InteractiveShellEmbed, I don't see why you apply docs of one function to
another.
> I don't want to inject variables to an isolated namespace, I want the console to share the application's namespace.
I did not tell you to pass an isolated namespace, I did tell you to pass `globals()` it is not isolated is is a reference to
current global namespace so it should be shared :
In [1]: a
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-60b725f10c9c> in <module>()
----> 1 a
NameError: name 'a' is not defined
In [2]: def fun(ns):
...: ns['a'] =1
...:
In [3]: fun(globals())
In [4]: a
Out[4]: 1
IIRC `embed` get globals/local by sys.get_frame and pulling from caller (where we can know the stack depth).
Like that:
In [1]: import sys
In [2]: b
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-3b5d5c371295> in <module>()
----> 1 b
NameError: name 'b' is not defined
In [3]: def fun(): sys._getframe(0).f_back.f_globals['b']=12
In [4]: fun()
In [5]: b
Out[5]: 12
So here, you just need to do it yourself (example a), as the kernel is potentially on a different process
we don't do any magic (as in b) I guess we could, but this part of the code start to look like some dark magic.
--
M
>
> On Sat Dec 27 2014 at 6:02:54 PM Matthias Bussonnier <bussonniermatthias at gmail.com> wrote:
>
> Le 27 déc. 2014 à 23:43, Darren Dale <dsdale24 at gmail.com> a écrit :
>
>> Thanks Ray. Unfortunately, I want the ipython console's namespace to reflect that of the application in which it is embedded, not a specific widget. There is an example that appears to do this at https://github.com/ipython/ipython/blob/master/examples/Embedding/embed_function.py , but I'm trying to do the same with the Qt widget. At the top of my main gui module, I do:
>>
>> try:
>> from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
>> from IPython.qt.inprocess import QtInProcessKernelManager
>>
>> # Create an in-process kernel
>> kernel_manager = QtInProcessKernelManager(user_ns=None)
>
> Don't you want to just pass `globals()` instead of `None` here?
> user_ns kwarg here is **the mutable namespace** (i.e. a dict) you want your kernel to work on. If you pass None,
> it will create a new empty object for you. If you pass `{"a":1}` then your kernel will see a variable `a` with a value of 1 (mutable)
>
> So I guess what you want to pass here is either globals(), locals(), or a dict you constructed yourself,
> from the bit you want available.
>
> You can also do as raymond said, and inject things into user_ns after getting the reference to it.
> --
> M
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20141228/5ac145fe/attachment.html>
More information about the IPython-dev
mailing list