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
[email protected]

On Wed, Feb 8, 2017 at 11:55 PM, DG <[email protected]> wrote:

> 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
>
>
>
> On Wednesday, February 8, 2017 at 2:28:57 AM UTC-8, Johannes Feist wrote:
>>
>> 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
>>
>> Documentation is at http://nbconvert.readthedocs.i
>> o/en/stable/usage.html#convert-notebook
>>
>> Johannes
>>
>> On Wed, Feb 8, 2017 at 5:40 AM DG <[email protected]> wrote:
>>
>>> 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/session
>>> s'))
>>> 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]> 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/ms
>>> gid/jupyter/192cebe0-4e5a-4250-9a80-22018744f419%40googlegroups.com
>>> <https://groups.google.com/d/msgid/jupyter/192cebe0-4e5a-4250-9a80-22018744f419%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> 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
>> [email protected]
>>
> --
> 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/ee3f36ec-349f-453b-ae09-5f177dd08237%40googlegroups.com
> <https://groups.google.com/d/msgid/jupyter/ee3f36ec-349f-453b-ae09-5f177dd08237%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAPERba1r28SFtC5%2BEbyuEV2oVg2CPrtLDht%2BeKY4MT%2BcS1EZtg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to