0

I currently use Spyder to generate some plots that are shown in Spyder's IPython console. This allows me to save the output as an HTML file by right-clicking on the IPython console and selecting 'Save as HTML/XML'.

Say I have the following code:

import matplotlib.pylab as plt
print "Some text."
plt.plot([1,6,3,9,5])
plt.title('Some values')

Now I want to run this script remotely via SSH. How can I generate an equivalent html file with the output that would normaly be generated in the IPython console without opening Spyder? The -X parameter of ssh can be used. I am looking for an answer that is not specific to the code above but works for any kind of plot/output.

Ohumeronen
  • 1,769
  • 2
  • 14
  • 28

1 Answers1

1

For what I gathered you want to save the output of an interactive iPython console to html. I couldn't find a direct way of right-clicking and saving to HTML (I'm sure if it's possible to do using a UI command it's possible to do without, I haven't looked carefully).

A workaround however could be first saving it to an *.ipynb and then from the shell coverting it to *.html

Assuming you have a file called test.py that you want to run you just add the following code to it:

import IPython.nbformat.current as nbf
nb = nbf.read(open('test.py', 'r'), 'py')
nbf.write(nb, open('test.ipynb', 'w'), 'ipynb')

from this answer

then you can just use a shell to:

ipython nbconvert --to html test.ipynb

If you want to save a session that you are running and not a saved file you should first use the %save 'test.py' magic command and then do the previous steps.

Community
  • 1
  • 1
nico
  • 385
  • 4
  • 19
  • Thank you for your suggestion. I saved your 3 lines of code to a file nbf.py and ran it like 'python nbf.py'. It creates a file test.ipynb. Then I convert the file with the shell command you provided. Both the .ipynb file and the .html file contain the sourcecode of the script test.py (provided above). What I wanted is to save the output of this script in an html file. I don't understand your last suggestion: %save 'test.py'. Is this an extension to the shell code? Thank you again for your effort. It would be very nice to get it working. – Ohumeronen Jul 19 '16 at 11:48
  • 1
    Actually it doesn't save the output just the input if you run it from a *.py. It should work if you are running an interactive console and executing each line manually. The %save is a magic command, the %history I think can be useful also. You can read about magic commands here - http://ipython.readthedocs.io/en/stable/interactive/magics.html – nico Jul 19 '16 at 12:13
  • You want to save the output of the console as it's being executed or just the plot would be enough? – nico Jul 19 '16 at 12:23
  • It would be nice to have both the textoutput and the plot in an html file but I was not even sure if this is possible when I asked the question. Do you think this is possible without a lot of work? If there is not easy solution I might just open the IDE via ssh -X even though this is a bit cumbersome. – Ohumeronen Jul 19 '16 at 12:31
  • 1
    there is also a %sc magic command that saves the output. I also found this link that looks promising: http://protips.maxmasnick.com/ipython-notebooks-automatically-export-py-and-html – nico Jul 19 '16 at 12:59
  • Thank you very much. I will have a look into the documents you provided to find a solution. – Ohumeronen Jul 19 '16 at 13:44