First time here? Check out the FAQ!

Ask Your Question
1

Import custom sage libraries into a Jupyter notebook

asked 8 years ago

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 8 years ago

mforets gravatar image

updated 8 years ago

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

...
Preview: (hide)
link

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: 2,201 times

Last updated: Feb 12 '17