Ask Your Question
1

Can I make a module out of a worksheet?

asked 9 years ago

stan gravatar image

updated 9 years ago

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?

Preview: (hide)

Comments

What worksheet is that ? ipynb ? sws ?

tmonteil gravatar imagetmonteil ( 9 years ago )

Sorry, forgot to mention: sws.

stan gravatar imagestan ( 9 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 9 years ago

tmonteil gravatar image

updated 9 years ago

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.

Preview: (hide)
link

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 ( 9 years ago )

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: 9 years ago

Seen: 397 times

Last updated: Apr 06 '16