Save Ipython notebook as script with the same filename

# creating a variable theNotebook with the name of notebook
# source: http://stackoverflow.com/a/23619544
# In[1]:

%%javascript
var kernel = IPython.notebook.kernel;
var thename = window.document.getElementById("notebook_name").innerHTML;
var command = "theNotebook = " + "'"+thename+"'";
kernel.execute(command);

# saving to a directory 'backup'. create the directory if it doesn't exist
# source http://stackoverflow.com/a/19067979
# In[2]:

try :
    if(__IPYTHON__) :
        print "saving", theNotebook
        import os
        dir_backup = 'backup'
        if not os.path.exists(dir_backup):
            os.makedirs(dir_backup)
        get_ipython().system(u'ipython nbconvert --to python {0} --output {1}'.format(theNotebook, os.path.join(dir_backup, theNotebook)) )
except NameError :
    print "Unable to save"

No comments:

Post a Comment