Ask Your Question
1

To call a code from outside a notebook

asked 2019-10-04 03:55:52 +0200

Cyrille gravatar image

updated 2019-10-04 03:56:27 +0200

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
3

answered 2019-10-04 21:02:12 +0200

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 21:09:43 +0200 )edit

Dear @tmonteil: I'm trying to do what you described above, but I cannot find the "Download as Python" option in the File menu of the Sage notebook. I'm currently using version 7.6. Is this a feature of newer versions, or am I doing something wrong?

Jose Brox gravatar imageJose Brox ( 2020-01-23 15:15:55 +0200 )edit
2

answered 2019-10-06 21:04:56 +0200

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-04 03:55:52 +0200

Seen: 289 times

Last updated: Oct 06 '19