ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 26 Apr 2016 16:28:08 +0200store python code from a notebook cell to a filehttps://ask.sagemath.org/question/33221/store-python-code-from-a-notebook-cell-to-a-file/ 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.Mon, 25 Apr 2016 00:09:59 +0200https://ask.sagemath.org/question/33221/store-python-code-from-a-notebook-cell-to-a-file/Comment by kcrisman for <p>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.</p>
https://ask.sagemath.org/question/33221/store-python-code-from-a-notebook-cell-to-a-file/?comment=33225#post-id-33225Yes, 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.Mon, 25 Apr 2016 18:39:59 +0200https://ask.sagemath.org/question/33221/store-python-code-from-a-notebook-cell-to-a-file/?comment=33225#post-id-33225Answer by tmonteil for <p>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.</p>
https://ask.sagemath.org/question/33221/store-python-code-from-a-notebook-cell-to-a-file/?answer=33230#post-id-33230There 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)".
Tue, 26 Apr 2016 16:28:08 +0200https://ask.sagemath.org/question/33221/store-python-code-from-a-notebook-cell-to-a-file/?answer=33230#post-id-33230