Ask Your Question

store python code from a notebook cell to a file

asked 2016-04-25 00:09:59 +0200

bekalph gravatar image

During a notebook session I enter a series of SAGE-statements (python code) in a notebook cell. How can I let SAGE store/save this code automatically (not via copy&paste) in a file? The purpose is to reload the stored code in a cell of another session for later processing . I tried to perform this via the "save-session" -command. However, this results in a data volume of several MB, which is much larger than the total length of the code of the session. This I want to avoid.

edit retag flag offensive close merge delete


Yes, saving a session saves everything associated with it, which is a lot of Sage itself. I don't know that there is a way to do this other than simply cut and paste - though I'll point out you can go to "Edit" or something and cut and paste all at once.

kcrisman gravatar imagekcrisman ( 2016-04-25 18:39:59 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-04-26 16:28:08 +0200

tmonteil gravatar image

updated 2016-04-26 16:44:26 +0200

There is no simple solution with the Sage notebook. You can save your worksheet as an .sws file and then from the command line do sage -sws2rst your_worksheet.sws to get an rst file, so that it is easier to post-process the text file to obtain a genuine Python file (this can then easily be done since you just have to select lines that start either with <space><space><space><space>sage:<space> or with <space><space><space><space>...<space><space>).

It is easier with the jupyter notebook:

  • If you want a short notebook, you can clean all output by clicking on "Kernel" then "Restart & clear output". Then you can save your worksheet that only contains input cells (the cells with your code).
  • You can also get those input cells in a Python file by clicking on "Flie", then "Download as", then "Python (.py)".
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2016-04-25 00:09:59 +0200

Seen: 797 times

Last updated: Apr 26 '16