Ask Your Question

Can I make a module out of a worksheet?

asked 2016-04-06 07:24:54 -0500

stan gravatar image

updated 2016-04-06 07:25:28 -0500

I have been using save_session and load_session to re-use symbolic expressions and dictionaries across worksheets, but I still need to re-type all sorts of python functions (e.g. def fun()...), which cannot be pickled. Now I am wondering whether there is a way to automatically create a .py file out of a worksheet, i.e. a module that contains all code from a worksheet, excluding html comments. This could then be loaded as a module in other worksheets, allowing also to keep the namespaces separate, which would be awesome. Does anyone know of a script/method to do this?

edit retag flag offensive close merge delete


What worksheet is that ? ipynb ? sws ?

tmonteil gravatar imagetmonteil ( 2016-04-06 08:16:10 -0500 )edit

Sorry, forgot to mention: sws.

stan gravatar imagestan ( 2016-04-07 08:52:30 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-04-06 10:40:16 -0500

tmonteil gravatar image

updated 2016-04-06 12:41:29 -0500

If your worksheet is written in ipython/jupyter, you can do:

sage -ipython nbconvert --to script <your_worksheet>.ipynb

If your worksheet is written in sage_notebook, you can do:

sage -sws2rst <your_worksheet>.sws

In both cases, you will get some text file that is not exactly a python file, but from which it is easy to remove the very few additional stuff to get a python file.

edit flag offensive delete link more


Thanks, this seems to be a good start. I suppose that I need to remove all lines between those starting with sage: and those starting with .. end of output Plus I would need to remove all double colon lines. Is that correct? Should be possible to write a python script for this.

stan gravatar imagestan ( 2016-04-07 08:56:24 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2016-04-06 07:24:54 -0500

Seen: 154 times

Last updated: Apr 06 '16