Getting the port number in an IPython noteboook

2,489 views
Skip to first unread message

DG

unread,
Feb 6, 2017, 6:42:10 PM2/6/17
to Project Jupyter
Hello, can anybody please show a way to get the port number of the server connection in an IPython notebook command? I could not easily find this in the documentation. 

Thanks,
              dg

Carol Willing

unread,
Feb 6, 2017, 9:22:44 PM2/6/17
to jup...@googlegroups.com
Hi DG,

There are a few places to find the port number:

  1. Starting the notebook `jupyter notebook` displays the port in the console messages. [ref 1]
  2. Specifying a particular port number can be done using `jupyter notebook --port 8899` [ref 2]
  3. A port number can be specified in the `jupyter_notebook_config.py` file by adding `c.NotebookApp.port=8899`. [ref 3]
I hope this answers your question. If not, please do ask for more information.

Thanks,

Carol
February 6, 2017 at 3:42 PM
Hello, can anybody please show a way to get the port number of the server connection in an IPython notebook command? I could not easily find this in the documentation. 

Thanks,
              dg
--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/1ceb0932-1641-44a9-854c-f93a968d9835%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
Carol Willing

Research Software Engineer, Project Jupyter
Cal Poly San Luis Obispo

Director, Python Software Foundation

Strengths: Empathy, Relator, Ideation, Strategic, Learner

DG

unread,
Feb 6, 2017, 11:12:29 PM2/6/17
to Project Jupyter
Carol,
       thanks a lot for your informative and fast reply. 

Unfortunately, that's not what I am looking for. I need to get the port number from within an IPython cell. Maybe this isn't the right forum for this kind of question--I will try posting on the IPython group. 

Thanks again,
    DG

Matthias Bussonnier

unread,
Feb 7, 2017, 12:10:15 AM2/7/17
to jup...@googlegroups.com
Hi DG

On Mon, Feb 6, 2017 at 8:12 PM, DG <davide....@gmail.com> wrote:
> Carol,
> thanks a lot for your informative and fast reply.
>
> Unfortunately, that's not what I am looking for. I need to get the port
> number from within an IPython cell. Maybe this isn't the right forum for
> this kind of question--I will try posting on the IPython group.

That's seem like a strange thing to want, can you clarify your needs
and what you are trying to accomplish ?
Are you sure you want to get the notebook server port ? Or are you
looking for the ZMQ connect info ?
If the former, then you will need really weird hack (like publishing
javascript which lookup document.url and send it back to the kernel),
or make some shady assumption and peeking at /proc/$pid/ and uses some
heuristic. If the later you can use `%connect_info` magic when running
the IPython kernel, it should print the relevant informations.

Hope thats help, but a description of what you are trying to
accomplish might help.
--
M

Thomas Kluyver

unread,
Feb 7, 2017, 6:46:33 AM2/7/17
to Project Jupyter
On 6 February 2017 at 23:42, DG <davide....@gmail.com> wrote:
Hello, can anybody please show a way to get the port number of the server connection in an IPython notebook command? I could not easily find this in the documentation. 

By design, kernel code doesn't know about the notebook you're running it from. You can get a list of all running servers (notebook.notebookapp.list_running_servers()), but that doesn't indicate which one is handling the current code.

As Matthias suggested, please tell us a bit more about why you want to do this: we may be able to point you to a better option.

Thomas

DG

unread,
Feb 7, 2017, 11:40:17 PM2/7/17
to Project Jupyter
OK, here's where it comes from.

I have a data analysis script, residing in my Documents folder. The script reads raw data files on a server share. It creates a subfolder "Analysis" and puts a bunch of tables and plots in that folder. At the end of the script, I want to:
1. Save a copy of the executed notebook (.ipynb format) in the same subfolder, and
2. Save an HTML version of it, also in the same subfolder

This should be trivial to do but unfortunately it's not. (It's sad that such a wonderful tool is so lacking in terms of introspection). I currently handle those two tasks as follows. To create a copy of the notebook, I use 
        shutil.copy(nb_path,os.path.join(Save_dir,nb_filename))
To save an HTML copy, I use:
     !jupyter nbconvert '$nb_path' --output '$nfname'

Both of those require to know the path of the notebook currently running. After much googling, I settled on this:

connection_file_path = kernel.get_connection_file()
connection_file = os.path.basename(connection_file_path)
kernel_id = connection_file.split('-', 1)[1].split('.')[0]
sessions = json.load(urllib2.urlopen('http://127.0.0.1:8888/api/sessions'))
for sess in sessions:
    if sess['kernel']['id'] == kernel_id:
        nb_rel_path = (sess['notebook']['path'])
        break
res = !echo ~
nb_path = os.path.join(res[0],nb_rel_path)

This normally works, but the other day I had another server running, so naturally the second server's port was set to 8889 instead of 8888, therefore breaking my script.

There are two other problems. Sometimes the saved .ipynb is not complete. The last few cells are saved without the output. The sure way to save a complete notebook is to manually hit the Save button, then re-run the cell with the file copy command. So I looked for something as simple as issuing a "Save" command, but that also I could not find. 

The second problem is that code snipped does not seem to work in Windows...

Johannes Feist

unread,
Feb 8, 2017, 5:28:57 AM2/8/17
to Project Jupyter
Just in case you don't know about it, it sounds to me like this would be easier to do with nbconvert, using the execute preprocessor (which runs the notebook and saves it). E.g., you could do (from the command line)
jupyter nbconvert --to notebook --execute mynotebook.ipynb --output Analysis/mynotebook.ipynb
and then convert that executed notebook to html (output goes to the same folder):
jupyter nbconvert --to html Analysis/mynotebook.ipynb


Johannes

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

Johannes Feist
Departamento de Física Teórica de la Materia Condensada
Universidad Autónoma de Madrid
johanne...@uam.es

Thomas Kluyver

unread,
Feb 8, 2017, 8:52:33 AM2/8/17
to Project Jupyter
On 8 February 2017 at 04:40, DG <davide....@gmail.com> wrote:
I have a data analysis script, residing in my Documents folder. The script reads raw data files on a server share. It creates a subfolder "Analysis" and puts a bunch of tables and plots in that folder. At the end of the script, I want to:
1. Save a copy of the executed notebook (.ipynb format) in the same subfolder, and
2. Save an HTML version of it, also in the same subfolder

I think I'd agree with what Johannes said - using nbconvert with the --execute flag is probably the best option. It also makes it easy to script this, if you want to, for instance, have a Makefile controlling it.

DG

unread,
Feb 8, 2017, 5:55:51 PM2/8/17
to Project Jupyter
Hello Johannes, 
I will take a look at this approach, which looks more oriented to batch processing rather the interactive processing I do now.

However, right off the bat there are a few difficulties. The way my workflow looks like right now:
- Open data analysis IPython notebook (from within IPython Jupyter notebook home tab)
- change input variables (from which the path to the source files is calculated)
- run a few cells manually first to see if the output makes sense. Adjust some parameters of the analysis
- run notebook to completion, which includes saving the copy and the HTML version
- repeat from second step for additional data sets

I guess I can create another program or script that generates the needed command lines and then executes them. Will I be able to watch the output of the notebook in real time?

DG

Johannes Feist

unread,
Feb 9, 2017, 5:32:34 AM2/9/17
to Project Jupyter
Hi DG,

I see, I didn't get that your analysis is interactive in the first part. While I'm not a Jupyter developer, I think what you want could be achieved with some Javascript more easily. If you make a cell with

%%javascript
var nb = Jupyter.notebook;
nb.save_checkpoint();
nb.kernel.execute("NB_NAME = '" + nb.notebook_name + "'");

the "save_checkpoint" should ensure that your notebook is saved (AFAIK this is the same javascript that is executed when you click on "save and checkpoint"), and the last line saves the name of your notebook in the python variable "NB_NAME".
since shutil and the shell ("!") should use the notebook directory as the cwd automatically, I don't think you'll ever need the full working path? At least if your notebook server and kernel are running on the same system.
So then
shutil.copy(NB_NAME,os.path.join(Save_dir,NB_NAME))
!jupyter nbconvert '$NB_NAME' --output '$nfname'

should work. If nb.notebook_name is not enough information, maybe nb.base_url and nb.notebook_path are also relevant.

Best,
Johannes


Johannes Feist
Departamento de Física Teórica de la Materia Condensada
Universidad Autónoma de Madrid
johanne...@uam.es

To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.

To post to this group, send email to jup...@googlegroups.com.

Thomas Kluyver

unread,
Feb 9, 2017, 6:56:53 AM2/9/17
to Project Jupyter
On 8 February 2017 at 22:55, DG <davide....@gmail.com> wrote:
- Open data analysis IPython notebook (from within IPython Jupyter notebook home tab)
- change input variables (from which the path to the source files is calculated)

This sounds like you may be interested in nbparameterise, which can automate replacing input variables:
https://github.com/takluyver/nbparameterise

DG

unread,
Feb 11, 2017, 12:10:53 PM2/11/17
to Project Jupyter
Johannes, 
   thank you for showing me how to do a "save" operation. 

Could you please point out to me exactly where in the documentation did you find this information?


DG


Johannes Feist

unread,
Feb 13, 2017, 5:36:24 AM2/13/17
to Project Jupyter
Hi DG,
I didn't find it in any documentation - I'm not sure there is any for the JavaScript interface in the notebook(?). I recorded a profile in the Google Chrome debug tools of me clicking on the "save and checkpoint" menu entry and looked through the called function list afterwards...
Best,
Johannes

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Thomas Kluyver

unread,
Feb 13, 2017, 6:44:37 AM2/13/17
to Project Jupyter
On 13 February 2017 at 10:32, Johannes Feist <johanne...@gmail.com> wrote:
documentation - I'm not sure there is any for the JavaScript interface in the notebook(?).

Not really. We resisted documenting it for ages to avoid implying that it was stable (though of course many people, including ourselves, have built extensions on it anyway). Some specific bits are described, but not with detailed API docs - e.g. the frontend config system:
http://jupyter-notebook.readthedocs.io/en/latest/frontend_config.html

DG

unread,
Feb 18, 2017, 6:09:44 PM2/18/17
to Project Jupyter
OK, I found a solution:

%%javascript
var nb = Jupyter.notebook;
var port = window.location.port;
nb
.kernel.execute("NB_Port = '" + port + "'");


The above code will put the port (in string form) into the IPython variable NB_Port. I can then use it to build the correct URL to get the path to the current notebook, like this:

# Get file path of this notebook

connection_file_path
= kernel.get_connection_file()
connection_file
= os.path.basename(connection_file_path)
kernel_id
= connection_file.split('-', 1)[1].split('.')[0]

sessions
= json.load(urllib2.urlopen('http://127.0.0.1:'+NB_Port+'/api/sessions'))

for sess in sessions:
   
if sess['kernel']['id'] == kernel_id:
        nb_rel_path
= (sess['notebook']['path'])
       
break
res
= !echo ~
nb_path
= os.path.join(res[0],nb_rel_path)

nb_path
nb_dir
,nb_filename = os.path.split(nb_path)


This now works even as I have two Jupyter notebook sessions open,  one running Python 2.7 and one Python 3.6.Thanks to Johannes for showing me this method. 
Now, if I could find a way that works on Windows as well....

It's a shame that we have to do this.
Reply all
Reply to author
Forward
0 new messages