Ask Your Question

Get output of a .py file in a browser

asked 2019-04-03 10:19:52 -0500

Hi-Angel gravatar image

It seems like basic question, but I already spent about 2 hours, tried jupyter, command line, different combinations, different commands, lots of stuff…

Basically, I have file with one line of content, taken straight from docs:


I want to evaluate it, and get a picture of the plot in a browser.

What I don't want:

  • to open some external app for PNG files because in future I gonna need interactivity
  • to open the file in some text editor inside a browser because I already have it opened in Emacs
  • to get errors about graphs being undefined because when I execute in sage terminal, it's all correctly defined.
  • to search for imports to run every particular sage example from docs, because typing those examples directly in sage interactive interpreter works just fine (the examples don't include imports), so I see no reason to put an unnecessary effort over that.
edit retag flag offensive close merge delete


What operating system, and how was Sage installed?

slelievre gravatar imageslelievre ( 2019-04-18 12:19:55 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2019-04-03 14:54:16 -0500

slelievre gravatar image

updated 2019-04-03 14:57:06 -0500

If you don't want to add any import statement, rename your file to test.sage.

Let your file contain this:


If you want to keep the file as .py you will need the import statements.

In this case:

from sage.graphs.graph_generators import graphs

Note that the import statements are easy to figure out with the dedicated command.

sage: import_statements('graphs')
from sage.graphs.graph_generators import graphs
edit flag offensive delete link more


Adding .show() simply adds a text output Graphics object consisting of 36 graphics primitives, but I don't get it in a browser (or even in a PNG viewer for that matter). If that helps, when I run the same code directly from sage interactive prompt, I get a bit different output Launched png viewer for Graphics object consisting of 36 graphics primitives.

Hi-Angel gravatar imageHi-Angel ( 2019-04-04 01:32:53 -0500 )edit

Are you running a SageMath kernel in the notebook? Or a plain Python kernel?

Iguananaut gravatar imageIguananaut ( 2019-04-18 12:13:32 -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: 2019-04-03 09:21:06 -0500

Seen: 50 times

Last updated: Apr 03