Love using Jupyter notebooks, but after a while, they look like a...

@paulabartabajo_
Pau Labarta Bajo@paulabartabajo_
40 views Feb 15, 2024 ~2 min read
Advertisement
1
Love using Jupyter notebooks, but after a while, they look like a total mess? ๐Ÿ˜ตโ€๐Ÿ’ซ

What if I told you there is a quick, simple, and efficient way to make them tidy and shiny?

These 3 tips will help you keep your notebooks clean and boost your productivity ๐Ÿš€โ†“
2
Jupyter notebooks are the most popular environment to develop Machine Learning models.

They are the faster way to
โ†’ add code
โ†’ fix code
โ†’ re-run code

for your Machine Learning project.

However, they quickly turn into a mess...

... unless you follow these 3 tips.
3
Tip #1. Encapsulate common code as functions.

If you do not encapsulate your code, you are doomed to duplicate it.

And code duplication is both a productivity killer and an endless source of bugs.

The solution:
โ†’ Define functionality ONCE.
โ†’ Call it as many times as you need
4
Tip #2. Extract common functions into a separate src/ folder

Often you have the same function defined in several notebooks. Which is, again, code duplication.

To solve this create a source code folder (aka src/) at the same level where your notebooks are...
5
...and extract your functions as code in .py files.

You can group these functions into separate .py files, depending on their main functionality:

โ†’ Plotting
โ†’ Data transformation
โ†’ Model training
โ†’ Utils

Your project structure will look like this โ†“
Media image
6
Tip #3: Add `autoreload` magic to your Jupyter notebook.

To use your functions inside Jupyter, you need to import them.

For example:
Media image
7
By default, Jupyter caches all library imports, and only loads them once, unless you restart the kernel.

So when you update the .py files in src/, the changes are not picked up by Jupyter.

To solve this, you just add these 2 lines at the beginning of your notebook.
Media image
8
To sum up:

โ†’ Reduce code duplication, by encapsulating and extracting common functionality into separate functions under src/.

โ†’ Add autorelaod magic to your notebook, to keep it in sync with your src/ code.

And Voila!
9
Wanna get more tweets like this?
โ†’ Follow me @paulabartabajo_

Wanna help me spread the word?
โ†’ Like/Retweet the first tweet below โ†“โ†“โ†“
@paulabartabajo_
Pau Labarta Bajo@paulabartabajo_
Love using Jupyter notebooks, but after a while, they look like a total mess? ๐Ÿ˜ตโ€๐Ÿ’ซ

What if I told you there is a quick, simple, and efficient way to make them tidy and shiny?

These 3 tips will help you keep your notebooks clean and boost your productivity ๐Ÿš€โ†“
10
Join 2k members to the ๐—ฆ๐—ฒ๐—ฟ๐˜ƒ๐—ฒ๐—ฟ๐—น๐—ฒ๐˜€๐˜€ ๐— ๐—Ÿ ๐—–๐—ผ๐—บ๐—บ๐˜‚๐—ป๐—ถ๐˜๐˜† ๐Ÿค—

A Discord community of ML builders ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จ๐Ÿฝโ€๐Ÿ’ป focused on building ๐—ฟ๐—ฒ๐—ฎ๐—น-๐˜„๐—ผ๐—ฟ๐—น๐—ฑ ๐— ๐—Ÿ ๐—ฎ๐—ฝ๐—ฝ๐˜€
โ†“โ†“โ†“
serverless-ml.carrd.co
Actions
What You Can Do
  • Export as PDF or Markdown
  • Batch Export to Notion
  • Bookmark & Highlight
  • Screenshot Tweet
Create Free Account

Includes 7-day Premium trial

Advertisement