[IPython-dev] Get Notebook name in python

William Stein wstein at gmail.com
Thu Jun 5 17:01:10 EDT 2014


On Tue, Jun 3, 2014 at 2:10 PM, MinRK <benjaminrk at gmail.com> wrote:

> The kernel does not know that a notebook is associated with it because
> there can be many frontends associated with the same kernel. If you really
> need it for some reason, you could fetch it from the frontend via
> javascript or a widget.
>
>
Motivated by this question, I just implemented this functionality for
SageMathCloud (SMC) worksheets.  I'm just going to mention what I did here,
in the hope that maybe IPython will do something similar (to minimize user
confusion).     When a SageMathCloud worksheet is started, the backend runs
a small amount of initialization code, mainly to set the current directory.
 I just added code so that it sets the variable __file__ in the namespace
in which the worksheet is executed.   This is similar to how doing python
foo.py sets __file__ to "foo.py" inside the foo module, and is I suspect
the first thing a Python-programmer would guess would work.

As with IPython, with SMC the Python process executing code knows nothing
about files/worksheets.  Still, the fact is that in some cases that
code-executing Python process was started in order to run code in a
worksheet, so it seems fine to me for that process to be informed of the
worksheet's filename.

Incidentally, I made __file__ the absolutely (rather than relative) path to
the worksheet, due to the discussion  [1].  I also made __file__ a unicode
string, since very often people's filenames are unicode in SMC.

[1]
http://stackoverflow.com/questions/7116889/python-file-attribute-absolute-or-relative

 -- William


>
> On Tue, Jun 3, 2014 at 2:01 PM, Jakob Gager <gager at ilsb.tuwien.ac.at>
> wrote:
>
>> Hy guys,
>>
>> is there a way to get the current notebook name in python? Something
>> similar to
>> the javascript IPython.notebook.notebook_name.
>>
>> Thanks for the help!
>>
>> br
>> Jakob
>> _______________________________________________
>> 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
>
>


-- 
William Stein
Professor of Mathematics
University of Washington
http://wstein.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20140605/9443d08a/attachment.html>


More information about the IPython-dev mailing list