Ask Your Question
1

To call a code from outside a notebook

asked 5 years ago

Cyrille gravatar image

updated 5 years ago

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 ?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
3

answered 5 years ago

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.

Preview: (hide)
link

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 ( 5 years ago )

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 ( 5 years ago )
2

answered 5 years ago

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.

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: 5 years ago

Seen: 339 times

Last updated: Oct 06 '19