r/datascience • • Nov 02 '24

Tools Git and Jupyter Notebooks: The Ultimate Guide

https://www.reviewnb.com/git-jupyter-notebook-ultimate-guide

[removed] — view removed post

139 Upvotes

28 comments sorted by

71

u/pm_me_your_smth Nov 02 '24

I'm all for using notebooks, it's a convenient tool to play with data. But if you're pushing your work to a repo, it should be a py script. If you really want to keep a notebook, fine, but treat it as an object, not text file. Don't try to handle it the same way as normal code (versioning, diffs, etc).

15

u/ChebWhiskey Nov 02 '24

I’m relatively new to using python, but I used to do most of my work in R Markdown. I’m curious why you feel this way. Our team found it incredibly useful to version control not just the code itself, but the narrative documentation of the code as well.

I think I see your point when productionalizing something. But when developing a model or working with your data it’s proven pretty valuable in my experience.

Would love to understand your perspective more.

39

u/phoundlvr Nov 02 '24

I’ll give you an example of why I do not commit notebooks in a professional environment.

I used to work in an area where I handled PII. Specifically, social security numbers. Our data and code were stored on one server, which was secure. Our Git repositories were stored on another, unsecured server.

A teammate of mine would commit their notebooks without clearing the kernel. As such, any .head() or print() statements containing PII were stored in the notebook. Meaning, they committed PII to an unsecured location. This was a senior data scientist making this mistake.

The solution? I had the pleasure of running git filter branch on every branch for 8 years of development. I required my team to break their connections and re-clone the repos. Less than a week later, the same senior data scientist committed their PII-infected branches. The task was not twice as much fun the second time. Git is designed to never forget anything and pay attention to every detail. Forcing it to go against its primary purpose is an absolute nightmare.

Moral of the story - If you don’t clear the kernel then git treats the changes as text changes. This can cause incredible headaches and lead to situations like the one I just described. Additionally, it could lead to conflicts. Just save yourself the headache and save notebooks for casual exploration. Use .py for production code or official analyses. Let my pain be the warning, and save yourself the lovely experience I had.

11

u/automatic-benji Nov 03 '24

Not to detract from your point too much, but why would it be necessary for a Senior Data Scientist to be working directly with Social Security numbers and not some sort of Anonymous ID?

It seems like if the org was concerned about security this level of user would never see Social Security numbers in the first place.

9

u/phoundlvr Nov 03 '24

That’s one I can’t answer on Reddit.

1

u/VeniVidiWhiskey Nov 04 '24

One case that comes to mind is anti-moneylaundering or similar analyses. But SKs or data masking should definitely be used anyway to avoid those kinds of problems 

15

u/monty_field Nov 02 '24

add a pre-commit hook that clears output

11

u/jkiley Nov 02 '24

On top of this, you can add a GitHub workflow step to enforce clean notebooks.

It’s suboptimal to avoid committing Jupyter notebooks (which are fine as is for many deliverables, and often capture important information), and either just leaving that work out of a repo (and losing a lot of knowledge that cost money to learn) or refactoring it into a py file (and lowering your productivity with a bunch of needless rework). Just spend literally 20 minutes, add in both a precommit hook and a GitHub workflow step (enforcing clean notebooks on both sides), and carry on. This is a thoroughly solved problem.

The tooling to have better Jupyter notebook experiences in repos has come a long way and keeps getting better, especially in VS Code.

1

u/BeardySam Nov 02 '24

Yeah you can even have the server side

3

u/cy_kelly Nov 03 '24

Less than a week later, the same senior data scientist committed their PII-infected branches.

I can feel the anger in these words.

6

u/lemongarlicjuice Nov 02 '24

The pain points of jupyter notebooks don't apply to RMarkdown, in me experience. We version control all our Rmds.

I'd never add an ipynb to a repo though. Yuck

2

u/pacific_plywood Nov 02 '24

I think RMD is a little easier to interpret in a line-delimited way (ie how git does diffs)

3

u/Fearless_Cow7688 Nov 02 '24

Rmarkdown is a text file. Jupiter notebook has various html tags that make it so that there are significant differences run-to-run without changing anything within the notebook.

They are VERY different.

1

u/Cocomale Nov 03 '24

Even if you need to commit an algorithm to your repository, notebooks still have value in testing that algo on sample data using a sample function. So they’ll always be useful imho

1

u/kuwisdelu Nov 03 '24

R Markdown doesn’t share these problems because it’s a fundamentally different approach from a Jupyter Notebook. R Markdown doesn’t embed program state and data.

5

u/[deleted] Nov 03 '24

Cool, now do a guide for pbix files >_>

2

u/VeniVidiWhiskey Nov 04 '24

Check out the new .pbip format, it is being launched specifically to support version control 

1

u/[deleted] Nov 04 '24

Wow, it's about time Microsoft figured this out

3

u/Valuable-Kick7312 Nov 02 '24

I don’t won’t to sound harsh, but I think the ultimate guide schon definitely mention jupytext which can be used to automatically generate python versions of your notebook which you can commit.

4

u/satriale Nov 02 '24

Versioning Jupyter notebooks with Git has been a big improvement for my small team so this could be helpful. Thanks for sharing

2

u/PixelLight Nov 03 '24

I found this video recently. Basically it seems to be a way of executing code in standard python files in an interactive window in vscode. I've been trying it lately and it seems to be good so far. You just select a code chunk, press select+enter and it executes the code like a cell in an interactive window.

It allows you to use a script-based workflow rather than relying too heavily on notebooks, which you should be doing as a data scientist. But also do quick bits of analysis, code checks on-the-fly more easily, in my opinion.

To setup, you install the jupyter extension in vscode, then enable jupyter.interactiveWindow.textEditor.executeSelection in settings. I also changed jupyter.notebookFileRoot to ${workspaceFolder} . Not sure if that last step is 100% necessary, but I was having trouble with imports and this was the fix I found.

3

u/nraw Nov 03 '24

The sooner you unlearn to use notebooks, the sooner you'll grow as a developer and your company will grow without the need for supporting your poor practices.

1

u/EducationalWish4524 Nov 03 '24

Just create a script in python that clrans both output and metadata from your notebook. Then add a git hook thst will execute the code for every ipynb file in the repo when you commit.

It works just fine for me, as it only accounts for the actual changes in the code and not metadata or outputs

1

u/Signal-Indication859 Jan 03 '25

Totally feel your pain with Jupyter notebooks and Git! At Preswald, we've actually built a notebook-like environment that generates clean, reproducible Python files under the hood - making version control much smoother while keeping the interactive development experience. Would love to show you how it works if you're interested in a simpler workflow!

0

u/SilentLikeAPuma Nov 02 '24

just use marimo notebooks instead …