How to rename a Jupyter Notebook programmatically? - jupyter-notebook

Is there a command or any other way to rename a Jupyter Notebook in execution time?
Like appending a string created while running the notebook to the notebook's name.

As answered in this other question, one can simply call the javascript magic command and Jupyter.notebook.rename().
%%js
Jupyter.notebook.rename(newNameString)

Related

Python 3 and files are missing in Jupyter notebook

When I open my Jupyter notebook, the files are missing, and I cannot create a python 3 file. And when I click the text file, this message Appears "'_xsrf' argument missing from POST"
There are a few potential causes for this problem.
One possibility is that the Jupyter notebook server is not running.
Another possibility is that the Jupyter notebook server is not configured to use Python
Finally, it is also possible that the Jupyter notebook server is not able to find the Python 3 interpreter.

Does Jupyter Notebook have a file rename hook?

I've written a custom pre_save_hook that's similar to the one that the Jupyter Notebook docs provide. The one I've written adds metadata to a notebook when the notebook is saved. However, the pre_save_hook doesn't seem to get called when a notebook is renamed. Is there something similar to the pre_save_hook for renaming a notebook?

How do I set the save location of a session in Jupyter notebook?

I'm working with Jupyter Notebook version 4.1.0 and can't seem to manually select the save path.
How does one manually specify where a session is saved?
You can launch jupyter notebook from the command line from inside the desired folder path. This will set the current folder as the start of the directory tree in Jupyter.
If we already have a session running, we can select the option IPython Notebook (.ipynb), from the file menu, under the Download as option. After saving the Notebook to a desired path, we can open it using the method described above.

Save ipython notebook as script programmatically

The excellent ipython notebook has a handy --script command line flag that automatically saves a copy of the notebook as a .py script file (removing any header and markdown cells). Is there a way to switch this feature on from inside the notebook itself after the notebook is opened? Apparently, this option is not accessible to the %config magic.
Is there a way to have a cell that does this conversion? Is there any command-line tool I could use to do the conversion, and just have that in a shell command run from the notebook? (It seems that nbconvert does not output to .py.)
The reason I ask is that I have a git repository of notebooks, and I need to make sure the .py files are kept up to date when users change the notebooks themselves because the .py files are used to create c++ code from the contents of the notebooks. But I can't rely on users to set the --script flag because they'll always forget. (And I include myself in that group of users.)
Better yet (at least for my purposes): ipython respects local copies of the ipython_notebook_config.py file. So I can just add
c = get_config()
c.NotebookManager.save_script = True
to such a file in my notebook directory. Apparently, ipython first reads ~/.ipython/profile_default/ipython_notebook_config.py, and then reads the local copy of that file. So it's safe to use without worrying about demolishing the user settings.
This was not at all clear to me from the documentation, but I just tried it and it worked.
Oh. My mistake. nbconvert can handle conversions to script. So I can do something like this:
!ipython nbconvert --to python MyNB.ipynb
Of course, this line will get saved to the script, which means the script will try to re-save the notebook to itself every time it's executed. That's a bit circular, and I can imagine it could cause problems with some of my more outlandish hacks. Instead, we can ensure that it's only run from ipython by wrapping it as follows:
try :
if(__IPYTHON__) :
!ipython nbconvert --to python MyNB.ipynb
except NameError :
pass
Note that the conversion process will automatically convert the ! syntax to something that is acceptable to plain python. This is apparently not the case with the --script conversion. So the extra-safe way to do this is
try :
if(__IPYTHON__) :
get_ipython().system(u'ipython nbconvert --to python MyNB.ipynb')
except NameError :
pass

IPython Notebook: Save the currently running notebook as ipynb file using a python command in a cell?

Is there are a way to save the Ipython notebook as an ipynb file from a cell within that notebook?
I know I can save it at any time by manually pressing "CTRL-M S", but I would like to use a command in a cell to do so (python command or %magic).
In this way I could "Run all cells" and be sure that the output (e.g. inline figures) is saved into the notebookfile when the execution is finished.
Update: Current versions of the Jupyter notebook (the successor of the IPython notebook) autosave into a hidden folder every few minutes (This feature was in development when I asked the question - see accepted answer).
No, because the kernel does not know it is accessed from a notebook. Dev version have auto-save feature though, and you could write a javascript extension that listen for cell execution event. But Python is not the way to do it. (or display(Javascript('js-code-to-save-notebook')) in the last cell, but I did not tell you)

Resources