Ask Your Question

How can I share a Sage Interaction on a website without giving away the code?

asked 2016-11-11 18:06:12 +0200

markus gravatar image

I would like to add numerical examples coded in Sage (e.g. to my website without making available the code in the html.

edit retag flag offensive close merge delete


For reference: this question also asked at

kcrisman gravatar imagekcrisman ( 2016-11-11 18:22:12 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2016-11-11 23:08:59 +0200

updated 2016-11-12 07:02:41 +0200

One simple approach is to hide the code editor of the interact as described here to make the solution less obvious, but the code will still be in the page source.

A better way is to use the base64-encoding capabilities of the cell server. The permalink option of the "share" button produces a URL with the Sage code properly encoded. This GitHub gist describes a way to extract the original code from the encoded URL, which can then be displayed with eval() or exec().

The Python code in the gist doesn't ensure that the base64-string is a multiple of four, so you'll need to add =s as padding when needed. As an example, the permalink URL for plot(sin) is

The gist code splits the URL at = which is not quite right. If you just copy the z= portion of the URL up to the &, then you can display the plot in a SageCell using

from base64 import urlsafe_b64decode as decode  
from zlib import decompress
eval( decompress(decode('eJwryMkv0SjOzNMEABHuA1s=')) )

For multiline Python code, use exec() instead of eval(). You may also need an explicit show() for output you want to display, but you generally need that inside an interact on the cell server already.

You can also take the gist code as a starting point and directly encode and decode your original Sage code yourself.

edit flag offensive delete link more


Thank you, that works! I am still experiencing a problem with blank spaces. When I modify your example to run

import pandas

then the permalink works fine. But eval(decompress(decode('eJzLzC3ILypRKEjMS0ks5uUqyMkv0SjOzNMEAHDNCKQ=')))

returns the error: File "<string>", line 1 import pandas ^ SyntaxError: invalid syntax

markus gravatar imagemarkus ( 2016-11-12 05:38:36 +0200 )edit

Coming from a JavaScript perspective, I assumed eval there works the same in Python but it doesn't. For multiline code you need exec. Answer edited.

paulmasson gravatar imagepaulmasson ( 2016-11-12 07:00:24 +0200 )edit

Thanks, Paul!

markus gravatar imagemarkus ( 2016-11-12 08:12:03 +0200 )edit

Wow, we should add this to the documentation of the cell server in a more specific way!

kcrisman gravatar imagekcrisman ( 2016-11-14 19:06:19 +0200 )edit

answered 2016-11-11 21:23:57 +0200

tmonteil gravatar image

Give it away, give it away, give it away now !

edit flag offensive delete link more



I agree with the idea of sharing code. But this is for a course that I teach. So I don't want to give away the solution for obvious reasons

markus gravatar imagemarkus ( 2016-11-11 21:37:52 +0200 )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-11-11 18:06:12 +0200

Seen: 897 times

Last updated: Nov 12 '16