r/learnprogramming • • 21d ago

I'm confused why does jupyter have different extension not just .py

Like I get the point that it is a Jupiter notebook and you can have meta data and code separate but why does it need a full on different extension and everything . What happens if I convert the jupyter file into just plain .py? Does the text get automatically converted into comments if direct conversion it is even possible

75 Upvotes

30 comments sorted by

11

u/unwashed_but_washed 20d ago

Since nobody mentioned it:

nbconvert can be used to convert any .ipynb to .py or .md.

This is extremely useful if you'd like to have your jupyter notebooks saved on git, as ipynb files HATE version control due to all the meta-data they store.

So everyone telling you that they are different files is definitely right, however there is a way to do direct conversion, and oftentimes if using any kind of version control (i.e. git) You definitely do not want to commit the ipynb variants, instead committing the .md or .py variants instead. Also nbconvert can output to a bunch of other formats too, check out their docs!

2

u/Fluffy_white_fox 20d ago

Thanks I'll make sure to keep .ipynb out of git

73

u/vikmaychib 21d ago

From py to ipynb there are at least 2 layers. The jupyter notebook is an interface that exploits the ipython tool which in itself uses python. It is a neat little tool.

The key is in the extension: ipynb > Ipython notebook

If you are interested check out Ipython, it was a great tool that paved the way for notebooks to exist.

I think there are tools that allow you to convert notebooks to scripts but the results can be messy.

9

u/[deleted] 21d ago

[removed] — view removed comment

6

u/Fluffy_white_fox 20d ago

Depends professor to professor, but I generally go back and forth when I am creating the project but afterwards I want it to be simple .py

20

u/Living_Fig_6386 21d ago

Jupyter notebooks are not Python code. By convention, .py is used as a file extension for Python code.

A Jupyter notebook is actually a JSON file (JavaScript Object Notation), which is a format used with JavaScript and web technologies to represent data. It's basically a data structure which, at the top level, is a dictionary (like a JavaScript Object or a Python dict, but strictly written using JavaScript conventions).

The Jupyter notebook is a JSON object that has a bunch of metadata, and member called "cells" which is an array that corresponds to the individual sections of the notebook. Each cell has a type (markdown, code, stream output, display data, execute result, error, or raw), and each type is a dictionary with a structure specific to the cell type. There CAN be chunks of Python code mixed in there (or code in other languages; Jupyter supports plugin language kernels), but the notebook is not Python.

4

u/Fluffy_white_fox 20d ago

That clears up a lot of things .... Do you recommend jupyter or plain python files for pushing on to git? I have seen both styles online but I am confused on which is better to use and in which context

8

u/Living_Fig_6386 20d ago

Python for code, notebook when sharing notebooks (documents that embed code and the results of executing said code).

Notebooks are really intended as a way for someone to provide a narrative about a data analysis and show their work — they are not programs or libraries. Think of notebooks as a Word doc where you paste in code and the results of running it, except it’s easy for someone to tweak the pasted in code and update the results in place.

1

u/VastFlaky 20d ago

A program called Jupytext could help you. This can convert your ipynb files to plain python files for committing to git.

If you're working with JupyterLab I think there is also a setting to associate a notebook with its plain-text version and auto-update when the notebook content changes.

27

u/zeekar 21d ago edited 21d ago

... because it's not just Python code? if you try to run it with python filename.ipynb, Python will blow up with an error because you fed it garbage.

All of the stuff in the notebook that's not the code has to be stored, too. Jupyter knows how to read that data and separate it from the runnable code; the Python interpreter does not.

Your question is kind of like: "I'm confused. Why is Raisin Bran packaged in a different kind of box than raisins?"

7

u/Fluffy_white_fox 21d ago

Okay I got the point now and let's say I want to convert it without losing the text then what do I do

7

u/zeekar 21d ago

Convert it to what? If you want to make it a directly-executable Python script, what do you want to do with the rest of the text? Make it comments that only show up when you look at the code? Have it printed out before and after the embedded code runs?

4

u/Fluffy_white_fox 21d ago

Convert the jupyter file to normal py and the text blocks in it to comments if possible

11

u/No_Perspective_44 21d ago

You can use the tools that you were linked, but you could also use it as a learning experience. ipynb is a type of JSON, so you can check out the format and try to extract the relvant blocks and then write them to a regular py file. You could even try extracting some of the other types of value such as output and markdown and entering them in as comments to the py file.

4

u/Fluffy_white_fox 21d ago

That's actually a pretty good idea specially the last one I will surely try it out

1

u/N_T_F_D 21d ago

In jupyter in the menu do download as executable script, it does exactly that

217

u/IngmarBurgerman01 21d ago

Christ this sub man. Meant to be for learning programming but beginners just get shafted for asking a pretty valid question.

2

u/FreeLogicGate 21d ago

Seems like the OP got plenty of patient helpful answers.

The simple factual answer is that jupyter notebooks are not python programs. It's also pretty clear from the name, that they're a "notebook", and python is a programming language.

Jupyter was originally designed to support Julia, R and Python languages (which all figure in the name).

With the design of the Jupyter architecture, there's a Jupyter kernel for essentially every formally used computer programming language there is.

I'm ambivalent about threads like this, because the OP could easily have done a few minutes of research and had a far more focused question.

26

u/Icy_Butterscotch6661 20d ago

Bro got downvoted for answering

4

u/ohlaph 21d ago

Stack overflow has threads you can monitor.

2

u/funkdefied 20d ago

You’re asking the right question. Check out Marimo instead. 

1

u/Fluffy_white_fox 20d ago

I just checked it out ... And this is genuinely much better than how we were converting between notebook to py and even in plain text editor it in not over complicated just simple import of library, the constructor and decorators for cell . thanks really

3

u/ImBadlyDone 21d ago

File extensions help tell your computer how to read the data in the file, but they don't do anything about the actual data stored, which means changing the file extension doesn't convert the file type.

Python code is stored in plain text, which the computer can just read. Jupyter notebook are stored in JSON format, which the computer has to read and parse to extract all the code blocks and render them. Since they both are read and parsed differently it's convenient to have 2 different file extensions

It's like how even though .png and .jpeg both store image data, they have to have 2 different file extensions because the way they store data is different

2

u/mc_pm 21d ago

Have you opened one in a text editor and looked?

1

u/kitsnet 21d ago

Jupyter notebook can hold cell output (including graphical output) and thus can serve as a presentation tool. A regular Python script is not supposed to do so.

1

u/flashman1986 20d ago

Because it’s not a Python file. It. It is not valid Python syntax and therefore cannot be read by the Python interpreter

1

u/maxximillian 21d ago

You know how all squares are polygons but not all polygons are squares. Its kind of like that with a jupypter notebook. It's python code plus all the stuff that is needed to make the notebook. That ipynbgives you a certain amount of context just by seeing it at the end of a file name

0

u/[deleted] 21d ago

[deleted] — view deleted comment

6

u/Fluffy_white_fox 21d ago

Okay, I just opened it in a basic text editor and.... Since how long has there been so much json in there. Everything makes so much more sense now lol