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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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 - ipython.readthedocs.io/en/stable/interactive/magics.html
You want to save the output of the console as it's being executed or just the plot would be enough?
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.
there is also a %sc magic command that saves the output. I also found this link that looks promising: protips.maxmasnick.com/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.