Ask Your Question
1

Import custom sage libraries into a Jupyter notebook

asked 2017-02-10 21:16:55 +0200

as_butler gravatar image

What is the correct way to write and import custom SAGE libraries into a jupyter notebook?

When using jupyter notebooks with a python kernel, importing your own python library is as easy as saving a file foo.py in the same directory as the notebook and putting animport foo line in the notebook. Using a SAGE kernel, I can import foo.py into my notebook, but without the ability to call SAGE methods: e.g. calling the function

def monty(n): return SymmetricGroup(n)

from foo.py gives the error,

NameError: global name 'SymmetricGroup' is not defined

My desired workflow: work in a notebook for convenience but be able to pass on what I've done in the form of a library.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-02-12 14:43:31 +0200

mforets gravatar image

updated 2017-02-12 14:51:30 +0200

To import:

  • code from other files (.py, .sage, ..) into a notebook, this can be done simply with the load command, as in load('foo.py').

  • via standard python imports: say your library is in foo.sage, then

-

%%bash 

sage --preparse 'foo.sage' # will import sage library (sage.all_cmdline) = everything so that Sage functions can be called externally, into a new file foo.sage.py

mv foo.sage.py foo.py # let's rename it

-

from foo import monty

...
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

1 follower

Stats

Asked: 2017-02-10 21:16:55 +0200

Seen: 1,839 times

Last updated: Feb 12 '17