Ask Your Question
1

Can I make a module out of a worksheet?

asked 2016-04-06 14:24:54 +0100

stan gravatar image

updated 2016-04-06 14:25:28 +0100

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

Comments

What worksheet is that ? ipynb ? sws ?

tmonteil gravatar imagetmonteil ( 2016-04-06 15:16:10 +0100 )edit

Sorry, forgot to mention: sws.

stan gravatar imagestan ( 2016-04-07 15:52:30 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-04-06 17:40:16 +0100

tmonteil gravatar image

updated 2016-04-06 19:41:29 +0100

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

Comments

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 15:56:24 +0100 )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

Stats

Asked: 2016-04-06 14:24:54 +0100

Seen: 369 times

Last updated: Apr 06 '16