[IPython-User] query frontend?

Michael Kuhlen mqk@astro.berkeley....
Tue Apr 10 21:55:20 CDT 2012

I was afraid my question might not even make sense. Sorry about that.
My understanding of IPython clearly is still woefully incomplete.

> But maybe the better question is this: what exactly are you trying to do?

OK, in an attempt to validate my non-sensical question, let me explain
how it arose for me. Here's an abstracted version of my module (let's
call it my_module.py):

## beginning of module
def func(x,y):
    do stuff to x,y
    return result

def plotit(result):
    make a plot of result

import loadit as l  ### this loads a bunch of data from disk and takes
a long time.
do a whole bunch of manipulations of l.data resulting in x,y
## end of module

I usually use this interactively from an IPython shell, like so:
In [1]: import my_module as mm   ### this takes a long time because mm
imports the module loadit.
In [2]: result = mm.func(mm.x,mm.y)
In [3]: mm.plotit(result)
Fiddle with mm.plotit (or mm.func).
In [4]: reload(mm)  ### this does not take a long time, since loadit
was already imported previously.
In [5]: mm.plotit(result)
and so on.

Now I'm exploring switching to IPython notebooks. I've moved the
contents of loadit.py into its own cell, and then the manipulations
into a second cell. Now in the following cell I would like to do a
"from my_module import func, plotit" and then proceed as before. The
trouble is that that import triggers the import of the module loadit,
which takes a long time and duplicates my memory footprint. So I was
looking for something like

if not __imported_from_notebook__:
    import loadit as l
    do a whole bunch of manipulations of l.data resulting in x,y

in order to prevent the import of loadit when I'm using a notebook. Of
course I could simply get rid the "import loadit as l" and the
manipulations in my_module.py, but in the future I might want to
return to a non-notebook way of doing things, so I wanted to avoid
changing any of my existing modules, but it sounds like that may not
be an option in this case. I'll just go with commenting out those

Thanks in advance for indulging my idiosyncrasies.


On Tue, Apr 10, 2012 at 6:33 PM, Brian Granger <ellisonbg@gmail.com> wrote:
> Given the architecture of IPython, this doesn't even make sense to
> ask.  The kernel can have multiple different frontends attached to it,
> even simultaneously.  You could have a qtconsole and notebook attached
> to a single kernel and the kernel has absolutely no way of knowing
> which commands are coming from the qtconsole and which are coming from
> the notebook.  That the kernel knows nothing about the frontends is
> not an accident, but rather a deliberate design decision that I don't
> see us changing anytime soon.
> But maybe the better question is this: what exactly are you trying to
> do?  Maybe there is a different way of approaching it that would make
> more sense.  Or it might be that there are other abstraction IPython
> has or might have to help solve this issue.
> Cheers,
> Brian
> On Tue, Apr 10, 2012 at 5:24 PM, mqk <kuhlen@gmail.com> wrote:
>> Hi
>> Is there some way to find out from within a python module whether it is
>> being imported from a web notebook rather than from the command line?
>> Something akin to if __name__ == "__main__"?
>> Thanks,
>> Mike
>> --
>> View this message in context: http://python.6.n6.nabble.com/query-frontend-tp4790920p4790920.html
>> Sent from the IPython - User mailing list archive at Nabble.com.
>> _______________________________________________
>> IPython-User mailing list
>> IPython-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/ipython-user
> --
> Brian E. Granger
> Cal Poly State University, San Luis Obispo
> bgranger@calpoly.edu and ellisonbg@gmail.com
> _______________________________________________
> IPython-User mailing list
> IPython-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-user

*                                                                   *
*  Dr. Michael Kuhlen              Theoretical Astrophysics Center  *
*  email: mqk@astro.berkeley.edu   UC Berkeley                      *
*  cell phone: (831) 588-1468      B-116 Hearst Field Annex # 3411  *
*  skype username: mikekuhlen      Berkeley, CA 94720               *
*                                                                   *

More information about the IPython-User mailing list