Ask Your Question
0

Creating a file for user defined functions using JupyterLab environment.

asked 2020-11-02 17:49:22 -0600

Oshio gravatar image

updated 2020-11-03 14:25:40 -0600

FrédéricC gravatar image

I want to create a function in a separate file, and use that function in another one. Here is the example:

#my_file.ipynb

def my_function(n):
    return n*2

#main.ipnyb

attach('my_file.ipynb')
my_function(2)

But when doing this at the JupyterLab I receive the following erros:

(1) ValueError: unknown file extension '.ipynb' for load or attach (supported extensions: .py, .pyx, .sage, .spyx, .f, .f90, .m)

(2) NameError: name 'my_function' is not defined

This method seems to work for .sage files, but is not working for '.ipynb'.

There is some way to accomplish the result demonstrated through my example using only JupyterLab environment?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2020-11-04 07:50:44 -0600

Sébastien gravatar image

updated 2020-11-04 07:52:16 -0600

See the following page from the documentation of Jupyter notebook for Importing Jupyter Notebooks as Modules.

You may also consider just writing the code you want to import in a plain .py or .sage file in the same directory which allows you easily to import, run, load or attach to a jupyter notebook.

edit flag offensive delete link more

Comments

This may need adaptation to Sage (think : preparser...).

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-11-05 04:07:57 -0600 )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

Stats

Asked: 2020-11-02 17:49:22 -0600

Seen: 76 times

Last updated: Nov 04