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...


On Tuesday, February 7, 2017 at 3:46:33 AM UTC-8, takowl wrote:
>
> On 6 February 2017 at 23:42, DG <[email protected] <javascript:>> 
> 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
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jupyter/192cebe0-4e5a-4250-9a80-22018744f419%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to