Ask Your Question
0

To call a code from outside a notebook

asked 2019-10-03 20:55:52 -0500

Cyrille gravatar image

updated 2019-10-03 20:56:27 -0500

Suppose I have written a long piece of code. What is the standard procedure to call it from outside a notebook ?

An if it stays in the notebook, is there a way to hide an active cell ?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2019-10-04 14:02:12 -0500

tmonteil gravatar image

Regarding the first question, the ipynb notebook format is not executable, so you have to export your code as Python. For this, click on FIle then Download as then Python (.py).

Regarding the second quiestion (foldable cells), you have to install and configure some extensions. From a terminal, type

sage -pip install jupyter_contrib_nbextensions
sage -jupyter contrib nbextension install --user
sage -pip install jupyter_nbextensions_configurator
sage -jupyter nbextensions_configurator enable --user

Then run a jupyter, click on Edit then nbextensions config at the bottom, then click to select Collapsible Headings, and select the way you want it to work down the page.

edit flag offensive delete link more

Comments

Hello, @tmontiel. I think there has been a tiny confusion about Cyrille's intentions. What I understand is that he wants to use on a notebook some code that he has defined outside the notebook. I think the title of the title of his question should be "How to use in a notebook some code defined in an external file". I don't have privileges to edit others questions, but I think you do.

dsejas gravatar imagedsejas ( 2019-10-06 14:09:43 -0500 )edit
1

answered 2019-10-06 14:04:56 -0500

dsejas gravatar image

Hello, @Cyrille! Suppose you have some code written in a file called "my_code.py", and you want to use it in a Sage Notebook. All you have to do is use the command load("my_code.py").

Of course, this example assumes that you have created a notebook in the same folder as the file containing your code. If you have it some where else, you can specify its complete location as the argument of load. For example, suppose you you have your file in your home directory in Linux, you can write in your notebook the following:

load("/home/cyrille/my_code.py")

From the point where you use this command, you will be able to use the functions and subroutines defined in my_code.py.

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: 2019-10-03 20:55:52 -0500

Seen: 63 times

Last updated: Oct 06