r/learnprogramming • u/Fluffy_white_fox • 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
11
u/unwashed_but_washed 20d ago
Since nobody mentioned it:
nbconvertcan be used to convert any.ipynbto.pyor.md.This is extremely useful if you'd like to have your jupyter notebooks saved on git, as
ipynbfiles 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
.mdor.pyvariants instead. Alsonbconvertcan output to a bunch of other formats too, check out their docs!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.
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.
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?"
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/funkdefied 20d ago
You’re asking the right question. Check out Marimo instead.
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 name0
u/[deleted] 21d ago
[deleted] — view deleted comment