Best way to detect IPython notebook

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Best way to detect IPython notebook

TARUN GABA
Hi,
I have written a method, which behaves differently when called from IPython notebook, and from interpreter/qtconsole respectively.
I need to detect whether the method is called from notebook or interpreter.
This was the code I was using till now.

    if get_ipython().config['KernelApp']['parent_appname'] == 'ipython-notebook':
        ##Do something

But I have came to know that this particular dict changes its format in different IPython versions(I am not sure how many changes are there).

Is there any better method to achieve this?
Thanks in advance :)

_______________________________________________
IPython-dev mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Best way to detect IPython notebook

Matthias Bussonnier
Hi,

Le 20 févr. 2014 à 17:45, TARUN GABA a écrit :

> Hi,
> I have written a method, which behaves differently when called from IPython notebook, and from interpreter/qtconsole respectively.
> I need to detect whether the method is called from notebook or interpreter.
> This was the code I was using till now.
>
>     if get_ipython().config['KernelApp']['parent_appname'] == 'ipython-notebook':
>         ##Do something
>
> But I have came to know that this particular dict changes its format in different IPython versions(I am not sure how many changes are there).
>
> Is there any better method to achieve this?

You should describe what you ment by "called from a notebook"
it is not clear enough to give a correct answer.

But if the question is the same as usual, and as stated in many discussion scattered
over the net and mailing list, there is no, and there will be no reliable way to detect which
frontend are requiring code execution, as there is no reason for a kernel to be connected
to only one frontend.

It is like a book writer asking "how can I make the eye color of my main character the same as my reader" ?
The question make no sense as two person can be reading the same page of the same book at the same time.

And as usual, if you describe what you are trying to archive instead of how,
you might get an answer that suit you.


Cheers,
--
Matthias




> Thanks in advance :)
> _______________________________________________
> IPython-dev mailing list
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/ipython-dev

_______________________________________________
IPython-dev mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Best way to detect IPython notebook

Thomas Kluyver-2
In reply to this post by TARUN GABA
On 20 February 2014 08:45, TARUN GABA <[hidden email]> wrote:
    if get_ipython().config['KernelApp']['parent_appname'] == 'ipython-notebook':

Be aware that:
- This only tells you what started the kernel - you can start a kernel with the notebook and attach a Qt console to it, and there's no good way to tell which one has sent a piece of code.
- We want to get rid of that value eventually: https://github.com/ipython/ipython/pull/4980

As Matthias mentioned, can you tell us a bit more about what it is that you're trying to do?

Thanks,
Thomas

_______________________________________________
IPython-dev mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Best way to detect IPython notebook

Jason Moore
Tarun's has some code that starts a SimpleHTTPServer which runs a three.js app on a new port. He's been trying to have the code open a new browser tab if you run the code from the notebook. See the last cell here: http://nbviewer.ipython.org/github/PythonDynamics/pydy-tutorial-pycon-2014/blob/master/notebooks/n08_visualization.ipynb (once IPython 2.0 is out, we plan to have this optionally open up in a notebook cell). The code that does this is mostly here: https://github.com/pydy/pydy-viz/blob/master/pydy_viz/server.py

I think he was trying to detect whether you were in a notebook so that different behavior happens when the visualization server is opened. He'll have to comment more on the details.


Jason
moorepants.info
+01 530-601-9791


On Thu, Feb 20, 2014 at 2:41 PM, Thomas Kluyver <[hidden email]> wrote:
On 20 February 2014 08:45, TARUN GABA <[hidden email]> wrote:
    if get_ipython().config['KernelApp']['parent_appname'] == 'ipython-notebook':

Be aware that:
- This only tells you what started the kernel - you can start a kernel with the notebook and attach a Qt console to it, and there's no good way to tell which one has sent a piece of code.
- We want to get rid of that value eventually: https://github.com/ipython/ipython/pull/4980

As Matthias mentioned, can you tell us a bit more about what it is that you're trying to do?

Thanks,
Thomas

_______________________________________________
IPython-dev mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Best way to detect IPython notebook

Jason Moore


Jason
moorepants.info
+01 530-601-9791


On Thu, Feb 20, 2014 at 10:56 PM, Jason Moore <[hidden email]> wrote:
Tarun's has some code that starts a SimpleHTTPServer which runs a three.js app on a new port. He's been trying to have the code open a new browser tab if you run the code from the notebook. See the last cell here: http://nbviewer.ipython.org/github/PythonDynamics/pydy-tutorial-pycon-2014/blob/master/notebooks/n08_visualization.ipynb (once IPython 2.0 is out, we plan to have this optionally open up in a notebook cell). The code that does this is mostly here: https://github.com/pydy/pydy-viz/blob/master/pydy_viz/server.py

I think he was trying to detect whether you were in a notebook so that different behavior happens when the visualization server is opened. He'll have to comment more on the details.


Jason
moorepants.info
+01 <a href="tel:530-601-9791" value="+15306019791" target="_blank">530-601-9791


On Thu, Feb 20, 2014 at 2:41 PM, Thomas Kluyver <[hidden email]> wrote:
On 20 February 2014 08:45, TARUN GABA <[hidden email]> wrote:
    if get_ipython().config['KernelApp']['parent_appname'] == 'ipython-notebook':

Be aware that:
- This only tells you what started the kernel - you can start a kernel with the notebook and attach a Qt console to it, and there's no good way to tell which one has sent a piece of code.
- We want to get rid of that value eventually: https://github.com/ipython/ipython/pull/4980

As Matthias mentioned, can you tell us a bit more about what it is that you're trying to do?

Thanks,
Thomas

_______________________________________________
IPython-dev mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-dev




_______________________________________________
IPython-dev mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-dev