TheMP
TheMP

Reputation: 8427

How to interpret .py files as jupyter notebooks

I am using an online jupyter notebook that is somehow configured to read all .py files as jupyter notebook files: n

I am a big fan of this setup and would like to use it everywhere. On my own jupyter installation however, .py files are just interpreted as test files and are not by default loaded into jupyter cells. How can I achieve the same configuration for my jupyter notebook?

Upvotes: 7

Views: 729

Answers (3)

Roast Biter
Roast Biter

Reputation: 681

You use the python code in your Jupyter Notebook by just pasting the whole code in a cell OR :

%load pythonfile.py to load code from a file (not necessarily .py files) into a jupyter notebook cell;

%run pythonfile.py in order to execute the file instead of loading it (outputs whatever that file outputs).

Also, pythonfile.py should exist in the cd or you can use its full path.

Upvotes: 0

Karol Żak
Karol Żak

Reputation: 2406

What you're looking for is jupytext. You just need to install it into python env from which you're running your jupyter notebooks:

pip install jupytext --upgrade

And you get this:
enter image description here

Upvotes: 5

johannesack
johannesack

Reputation: 678

That's not exactly what you asked, but you can achieve something close to that by using the magic %load FILE.py in a new jupyter notebook.

%load FILE.py will copy the contents of FILE.py in the current cell upon executing it.

Upvotes: 1

Related Questions