Ask Your Question

How do I convert a notebook to a python script?

asked 2012-03-10 07:58:00 -0500

Alexandre Martins gravatar image

updated 2016-07-26 02:18:38 -0500

I'm asking this because I'm starting a project and Sage seems like a very good platform to do some prototyping.

Better yet, it might be a great way to use the code that results from my prototyping directly has the final version as well.

But for that to work with a minimal of hardship though, a few things would be usefull:

  1. A way to grab the code content of a notebook and generate an independent python script or function

  2. A list of libraries, with version, that every sage version uses.

  3. An instalable library version of sage that could be imported from the script

  4. Anything else that I'm missing and you know sage can do to help in this task

I'm not asking for a completely automatic way to do this, just for resources/scripts or general advices about how to do it.

I'm also just starting to look into sage so, don't assume to much on what I may know. ;)


edit retag flag offensive close merge delete


Both answers here are very usefull to me. I'm just taking a bit of time selecting a "right" answer because I haven't tried them yet in a big project.

Alexandre Martins gravatar imageAlexandre Martins ( 2012-05-11 00:29:16 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2012-03-10 11:28:30 -0500

Shashank gravatar image

There are a couple of ways of doing this. You can write a pythonscript script and import the sage module. In order to use sage commands use the following line

from sage.all import *

In order to do this you need sage in you PYHTONPATH. You can make sure it does by starting your python script with

import sys

A better alternative, which I use when I am trying to do what you are doing, is that I write the python script with 'from sage.all import *' on the first line and then use the python from sage to run it using the command

sage -python

This way you can use the python interpreter of sage.

edit flag offensive delete link more


I don't see how this answers the question.

Eviatar Bach gravatar imageEviatar Bach ( 2012-03-17 17:13:07 -0500 )edit

I was trying to answer point 1 and 3. I don't think the question was about how to copy paste the output.

Shashank gravatar imageShashank ( 2012-03-17 17:54:04 -0500 )edit

@EviatarBach This is actually a nice approach to what I was asking. Not a complete answer but I wasn't expecting that since what I ask is a bit outside sage's use cases.

Alexandre Martins gravatar imageAlexandre Martins ( 2012-05-11 00:27:17 -0500 )edit

answered 2012-03-17 17:18:27 -0500

Eviatar Bach gravatar image

Unfortunately, I don't think there's a direct way of doing this. However, you can click on the "Edit" tab on the top right of an open notebook. That will have all the code from that notebook, as well as the output. You can copy that, although you'll have to manually remove the output and the cell labels.

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: 2012-03-10 07:58:00 -0500

Seen: 701 times

Last updated: Jul 26 '16