Ask Your Question
3

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

asked 8 years ago

markus gravatar image

I would like to add numerical examples coded in Sage (e.g. https://wiki.sagemath.org/interact/) to my website without making available the code in the html.

Preview: (hide)

Comments

For reference: this question also asked at http://stackoverflow.com/questions/40...

kcrisman gravatar imagekcrisman ( 8 years ago )

2 Answers

Sort by » oldest newest most voted
3

answered 8 years ago

updated 8 years ago

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

http://sagecell.sagemath.org/?z=eJwryMkv0SjOzNMEABHuA1s=&lang=sage

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.

Preview: (hide)
link

Comments

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

import pandas
plot(sin)

then the permalink http://sagecell.sagemath.org/?z=eJzLz... works fine. But eval(decompress(decode('eJzLzC3ILypRKEjMS0ks5uUqyMkv0SjOzNMEAHDNCKQ=')))

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

markus gravatar imagemarkus ( 8 years ago )

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

Thanks, Paul!

markus gravatar imagemarkus ( 8 years ago )

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

kcrisman gravatar imagekcrisman ( 8 years ago )
2

answered 8 years ago

tmonteil gravatar image

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

Preview: (hide)
link

Comments

2

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 ( 8 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: 8 years ago

Seen: 1,284 times

Last updated: Nov 12 '16