Jupyter notebooks have been one of the vital controversial instruments within the information science neighborhood. There are some outspoken critics, in addition to passionate followers. However, many information scientists will agree that they are often actually helpful – if used properly. And that’s what we’re going to give attention to on this article, which is the second in my collection on Software program Patterns for Knowledge Science & ML Engineering. I’ll present you greatest practices for utilizing Jupyter Notebooks for exploratory information evaluation.
However first, we have to perceive why notebooks had been established within the scientific neighborhood. When information science was horny, notebooks weren’t a factor but. Earlier than them, we had IPython, which was built-in into IDEs corresponding to Spyder that attempted to imitate the way in which RStudio or Matlab labored. These instruments gained vital adoption amongst researchers.
In 2014, Mission Jupyter advanced from IPython. Its utilization sky-rocketed, pushed primarily by researchers who jumped to work in business. Nonetheless, approaches for utilizing notebooks that work properly for scientific tasks don’t essentially translate properly to analyses carried out for the enterprise and product items of enterprises. It’s not unusual for information scientists employed proper out of college to wrestle to meet the brand new expectations they encounter across the construction and presentation of their analyses.
On this article, we’ll discuss Jupyter notebooks particularly from a enterprise and product standpoint. As I already talked about, Jupyter notebooks are a polarising matter, so let’s go straight into my opinion.
Jupyter notebooks ought to be used for purely exploratory duties or ad-hoc evaluation ONLY.
A pocket book ought to be nothing greater than a report. The code it accommodates shouldn’t be essential in any respect. It’s solely the outcomes it generates that matter. Ideally, we should always be capable of cover the code within the pocket book as a result of it’s only a means to reply questions.
For instance: What are the statistical traits of those tables? What are the properties of this coaching dataset? What’s the affect of placing this mannequin into manufacturing? How can we ensure that this mannequin outperforms the earlier one? How has this AB check carried out?

Jupyter pocket book: pointers for efficient storytelling
Writing Jupyter notebooks is mainly a approach of telling a narrative or answering a query about an issue you’ve been investigating. However that doesn’t imply you need to present the specific work you’ve achieved to achieve your conclusion.
Notebooks must be refined.
They’re primarily created for the author to know a problem but in addition for his or her fellow friends to realize that information with out having to dive deep into the issue themselves.
Scope
The non-linear and tree-like nature of exploring datasets in notebooks, which generally comprise irrelevant sections of exploration streams that didn’t result in any reply, isn’t the way in which the pocket book ought to take a look at the top. The pocket book ought to comprise the minimal content material that greatest solutions the questions at hand. You must at all times touch upon and provides rationales about every of the assumptions and conclusions. Government summaries are at all times advisable as they’re excellent for stakeholders with a obscure curiosity within the matter or restricted time. They’re additionally a good way to organize peer reviewers for the total pocket book delve.
Viewers
The viewers for notebooks is often fairly technical or business-savvy. Therefore, you’re anticipated to make use of superior terminology. However, govt summaries or conclusions ought to at all times be written in easy language and hyperlink to sections with additional and deeper explanations. If you end up struggling to craft a pocket book for a non-technical viewers, possibly you need to contemplate making a slide deck as a substitute. There, you need to use infographics, customized visualizations, and broader methods to elucidate your concepts.

Context
At all times present context for the issue at hand. Knowledge by itself isn’t enough for a cohesive story. We have now to border the entire evaluation throughout the area we’re working in in order that the viewers feels comfy studying it. Use hyperlinks to the corporate’s present information base to help your statements and gather all of the references in a devoted part of the pocket book.
Easy methods to construction Jupyter pocket book’s content material
On this part, I’ll clarify the pocket book format I sometimes use. It could appear to be loads of work, however I like to recommend making a pocket book template with the next sections, leaving placeholders for the specifics of your job. Such a custom-made template will prevent loads of time and guarantee consistency throughout notebooks.
- Title: Ideally, the title of the related JIRA job (or another issue-tracking software program) linked to the duty. This enables you and your viewers to unambiguously join the reply (the pocket book) to the query (the JIRA job).
- Description: What do you need to obtain on this job? This ought to be very temporary.
- Desk of contents: The entries ought to hyperlink to the pocket book sections, permitting the reader to leap to the half they’re concerned about. (Jupyter creates HTML anchors for every headline which can be derived from the unique headline via headline.decrease().exchange(” “, “-“), so you possibly can hyperlink to them with plain Markdown hyperlinks corresponding to [section title](#section-title). You may as well place your individual anchors by including <a id=’your-anchor’></a> to markdown cells.)
- References: Hyperlinks to inner or exterior documentation with background info or particular info used throughout the evaluation offered within the pocket book.
- TL;DR or govt abstract: Clarify, very concisely, the outcomes of the entire exploration and spotlight the important thing conclusions (or questions) that you just’ve give you.
- Introduction & background: Put the duty into context, add details about the important thing enterprise precedents across the situation, and clarify the duty in additional element.
- Imports: Library imports and settings. Configure settings for third-party libraries, corresponding to matplotlib or seaborn. Add setting variables corresponding to dates to repair the exploration window.
- Knowledge to discover: Define the tables or datasets you’re exploring/analyzing and reference their sources or hyperlink their information catalog entries. Ideally, you floor how every dataset or desk is created and the way steadily it’s up to date. You may hyperlink this part to another piece of documentation.
- Evaluation cells
- Conclusion: Detailed rationalization of the important thing outcomes you’ve obtained within the Evaluation part, with hyperlinks to particular components of the notebooks the place readers can discover additional explanations.
Keep in mind to at all times use Markdown formatting for headers and to focus on essential statements and quotes. You possibly can verify the completely different Markdown syntax choices in Markdown Cells — Jupyter Pocket book 6.5.2 documentation.

Easy methods to manage code in Jupyter pocket book
For exploratory duties, the code to supply SQL queries, pandas information wrangling, or create plots isn’t essential for readers.
Nonetheless, it will be significant for reviewers, so we should always nonetheless keep a top quality and readability.
My suggestions for working with code in notebooks are the next:
Transfer auxiliary capabilities to plain Python modules
Usually, importing capabilities outlined in Python modules is best than defining them within the pocket book. For one, Git diffs inside .py recordsdata are approach simpler to learn than diffs in notebooks. The reader must also not must know what a operate is doing beneath the hood to observe the pocket book.
For instance, you sometimes have capabilities to learn your information, run SQL queries, and preprocess, rework, or enrich your dataset. All of them ought to be moved into .py filed after which imported into the pocket book in order that readers solely see the operate name. If a reviewer needs extra element, they’ll at all times take a look at the Python module straight.
I discover this particularly helpful for plotting capabilities, for instance. It’s typical that I can reuse the identical operate to make a barplot a number of occasions in my pocket book. I’ll must make small modifications, corresponding to utilizing a unique set of knowledge or a unique title, however the total plot format and elegance would be the identical. As an alternative of copying and pasting the identical code snippet round, I simply create a utils/plots.py module and create capabilities that may be imported and tailored by offering arguments.
Right here’s a quite simple instance:
import matplotlib.pyplot as plt
import numpy as np
def create_barplot(information, x_labels, title='', xlabel='', ylabel='', bar_color='b', bar_width=0.8, type='seaborn', figsize=(8, 6)):
"""Create a customizable barplot utilizing Matplotlib.
Parameters:
- information: Listing or array of knowledge to be plotted.
- x_labels: Listing of labels for the x-axis.
- title: Title of the plot.
- xlabel: Label for the x-axis.
- ylabel: Label for the y-axis.
- bar_color: Shade of the bars (default is blue).
- bar_width: Width of the bars (default is 0.8).
- type: Matplotlib type to use (e.g., 'seaborn', 'ggplot', 'default').
- figsize: Tuple specifying the determine measurement (width, top).
Returns:
- None
"""
plt.type.use(type)
fig, ax = plt.subplots(figsize=figsize)
x = np.arange(len(information))
ax.bar(x, information, colour=bar_color, width=bar_width)
ax.set_xticks(x)
ax.set_xticklabels(x_labels)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.set_title(title)
plt.present()
create_barplot(
information,
x_labels,
title=”Customizable Bar Plot”,
xlabel=”Classes”,
ylabel=”Values”,
bar_color=”skyblue”,
bar_width=0.6,
type=”seaborn”,
figsize=(10,6)
)
When creating these Python modules, keep in mind that the code remains to be a part of an exploratory evaluation. So except you’re utilizing it in another a part of the mission, it doesn’t should be excellent. Simply readable and comprehensible sufficient in your reviewers.

Utilizing SQL straight in Jupyter cells
There are some instances wherein information isn’t in reminiscence (e.g., in a pandas DataFrame) however within the firm’s information warehouse (e.g., Redshift). In these instances, many of the information exploration and wrangling will likely be achieved via SQL.
There are a number of methods to make use of SQl wit Jupyter notebooks. JupySQL permits you to write SQL code straight in pocket book cells and reveals the question consequence as if it was a pandas DataFrame. You may as well retailer SQL scripts in accompanying recordsdata or throughout the auxiliary Python modules we mentioned within the earlier part.
Whether or not it’s higher to make use of one or the opposite relies upon principally in your purpose:
For those who’re operating a knowledge exploration round a number of tables from a knowledge warehouse and also you need to present to your friends the standard and validity of the information, then displaying SQL queries throughout the pocket book is often the most suitable choice. Your reviewers will recognize that they’ll straight see the way you’ve queried these tables, what sort of joins you needed to make to reach at sure views, what filters you wanted to use, and many others.
Nonetheless, for those who’re simply producing a dataset to validate a machine studying mannequin and the principle focus of the pocket book is to point out completely different metrics and explainability outputs, then I’d advocate to cover the dataset extraction as a lot as doable and maintain the queries in a separate SQL script or Python module.
We’ll now see an instance of how you can use each choices.
Studying & executing from .sql scripts
We will use .sql recordsdata which can be opened and executed from the pocket book via a database connector library.
Let’s say we now have the next question in a select_purchases.sql file:
SELECT * FROM public.ecommerce_purchases WHERE product_id = 123
Then, we may outline a operate to execute SQL scripts:
import psycopg2
def execute_sql_script(filename, connection_params):
"""
Execute a SQL script from a file utilizing psycopg2.
Parameters:
- filename: The title of the SQL script file to execute.
- connection_params: A dictionary containing PostgreSQL connection parameters,
corresponding to 'host', 'port', 'database', 'consumer', and 'password'.
Returns:
- None
"""
host = connection_params.get('host', 'localhost')
port = connection_params.get('port', '5432')
database = connection_params.get('database', '')
consumer = connection_params.get('consumer', '')
password = connection_params.get('password', '')
attempt:
conn = psycopg2.join(
host=host,
port=port,
database=database,
consumer=consumer,
password=password
)
cursor = conn.cursor()
with open(filename, 'r') as sql_file:
sql_script = sql_file.learn()
cursor.execute(sql_script)
consequence = cursor.fetchall()
column_names = [desc[0] for desc in cursor.description]
df = pd.DataFrame(consequence, columns=column_names)
conn.commit()
conn.shut()
return df
besides Exception as e:
print(f"Error: {e}")
if 'conn' in locals():
conn.rollback()
conn.shut()
Notice that we now have supplied default values for the database connection parameters in order that we don’t must specify them each time. Nonetheless, keep in mind to by no means retailer secrets and techniques or different delicate info inside your Python scripts! (Later within the collection, we’ll talk about completely different options to this downside.)
Now we will use the next one-liner inside our pocket book to execute the script:
df = execute_sql_script('select_purchases.sql', connection_params)
Utilizing JupySQL
Historically, ipython-sql has been the software of selection to question SQL from Jupyter notebooks. Nevertheless it has been sundown by its authentic creator in April 2023, who recommends switching to JupySQL, which is an actively maintained fork. Going ahead, all enhancements and new options will solely be added to JupySQL.
To put in the library for utilizing it with Redshift, we now have to do:
pip set up jupysql sqlalchemy-redshift redshift-connector 'sqlalchemy<2'
(You may as well use it together with different databases corresponding to snowflake or duckdb,)
In your Jupyter pocket book now you can use the %load_ext sql magic command to allow SQL and use the next snippet to create a sqlalchemy Redshift engine:
from os import environ
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
consumer = environ["REDSHIFT_USERNAME"]
password = environ["REDSHIFT_PASSWORD"]
host = environ["REDSHIFT_HOST"]
url = URL.create(
drivername="redshift+redshift_connector",
username=consumer,
password=password,
host=host,
port=5439,
database="dev",
)
engine = create_engine(url)
Then, simply cross the engine to the magic command:
%sql engine --alias redshift-sqlalchemy
And also you’re able to go!
Now it’s simply so simple as utilizing the magic command and write any question that you just need to execute and you’ll get the leads to the cell’s output:
%sql
SELECT * FROM public.ecommerce_purchases WHERE product_id = 123
Make sure that cells are executed so as
I like to recommend you at all times run all code cells earlier than pushing the pocket book to your repository. Jupyter notebooks save the output state of every cell when it’s executed. That signifies that the code you wrote or edited may not correspond to the proven output of the cell.
Working a pocket book from prime to backside can also be a superb check to see in case your pocket book is dependent upon any consumer enter to execute appropriately. Ideally, every part ought to simply run via with out your intervention. If not, your evaluation is almost definitely not reproducible by others – and even by your future self.
A method of checking {that a} pocket book has been run in-order is to make use of the nbcheckorder pre-commit hook. It checks if the cell’s output numbers are sequential. In the event that they’re not, it signifies that the pocket book cells haven’t been executed one after the opposite and prevents the Git commit from going via.
Pattern .pre-commit-config.yaml:
- repo: native
rev: v0.2.0
hooks:
- id: nbcheckorder
For those who’re not utilizing pre-commit but, I extremely advocate you undertake this little software. I like to recommend you to start out studying about it via this introduction to pre-commit by Elliot Jordan. Later, you possibly can undergo its in depth documentation to know all of its options.
Filter cells’ output
Even higher than the tip earlier than, filter all cells’ output within the pocket book. One profit you get is you can ignore the cells states and outputs, however then again, it forces reviewers to run the code in native in the event that they need to see the outcomes. There are a number of methods to do that routinely.
You should utilize the nbstripout along with pre-commit as defined by Florian Rathgeber, the software’s writer, on GitHub:
- repo: native
rev: 0.6.1
hooks:
- id: nbstripout
You may as well use nbconvert –ClearOutputpPreprocessor in a customized pre-commit hook as defined by Yury Zhauniarovich:
- repo: native
hooks:
- id: jupyter-nb-clear-output
title: jupyter-nb-clear-output
recordsdata: .ipynb$
levels: [ commit ]
language: python
entry: jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace
additional_dependencies: [ 'nbconvert' ]
Produce and share stories with Jupyter pocket book
Now, right here comes a not very well-solved query within the business. What’s the easiest way to share your notebooks along with your group and exterior stakeholders?
By way of sharing analyses from Jupyter notebooks, the sphere is split between three several types of groups that foster alternative ways of working.
The translator groups
These groups consider that folks from enterprise or product items received’t be comfy studying Jupyter notebooks. Therefore, they adapt their evaluation and stories to their anticipated viewers.
Translator groups take their findings from the notebooks and add them to their firm’s information system (e.g., Confluence, Google Slides, and many others.). As a adverse aspect impact, they lose a few of the traceability of notebooks, as a result of it’s now tougher to assessment the report’s model historical past. However, they’ll argue, they can convey their outcomes and evaluation extra successfully to the respective stakeholders.
If you wish to do that, I like to recommend preserving a hyperlink between the exported doc and the Jupyter pocket book in order that they’re at all times in sync. On this setup, you possibly can maintain notebooks with much less textual content and conclusions, centered extra on the uncooked info or information proof. You’ll use the documentation system to increase on the manager abstract and feedback about every of the findings. On this approach, you possibly can decouple each deliverables – the exploratory code and the ensuing findings.
The all in-house groups
These groups use native Jupyter notebooks and share them with different enterprise items by constructing options tailor-made to their firm’s information system and infrastructure. They do consider that enterprise and product stakeholders ought to be capable of perceive the information scientist’s notebooks and really feel strongly about the necessity to maintain a totally traceable lineage from findings again to the uncooked information.
Nonetheless, it’s unlikely the finance group goes to GitHub or Bitbucket to learn your pocket book.
I’ve seen a number of options carried out on this house. For instance, you need to use instruments like nbconvert to generate PDFs from Jupyter notebooks or export them as HTML pages, in order that they are often simply shared with anybody, even exterior the technical groups.
You possibly can even transfer these notebooks into S3 and permit them to be hosted as a static web site with the rendered view. You may use a CI/CD workflow to create and push an HTML rendering of your pocket book to S3 when the code will get merged into a selected department.
The third-party software advocates
These groups use instruments that allow not simply the event of notebooks but in addition the sharing with different individuals within the organisation. This sometimes entails coping with complexities corresponding to guaranteeing safe and easy entry to inner information warehouses, information lakes, and databases.
A few of the most generally adopted instruments on this house are Deepnote, Amazon SageMaker, Google Vertex AI, and Azure Machine Studying. These are all full-fledged platforms for operating notebooks that permit spinning-up digital environments in distant machines to execute your code. They supply interactive plotting, information, and experiments exploration, which simplifies the entire information science lifecycle. For instance, Sagemaker permits you to visualise all of your experiments info that you’ve tracked with Sagemaker Experiments, and Deepnote presents additionally level and click on visualization with their Chart Blocks.
On prime of that, Deepnote and SageMaker mean you can share the pocket book with any of your friends to view it and even to allow real-time collaboration utilizing the identical execution setting.
There are additionally open-source options corresponding to JupyterHub, however the setup effort and upkeep that it is advisable function it isn’t value it. Spinning up a JupyterHub on-premises generally is a suboptimal answer, and solely in only a few instances does it make sense to do it (e.g: very specialised kinds of workloads which require particular {hardware}). By utilizing Cloud providers, you possibly can leverage economies of scale which assure a lot better fault-tolerant architectures than different firms which function in a unique enterprise can provide. You must assume the preliminary setup prices, delegate its upkeep to a platform operations group to stick with it and operating for Knowledge Scientists, and assure information safety and privateness. Subsequently, belief in managed providers will keep away from limitless complications concerning the infrastructure that’s higher not having.
My normal recommendation for exploring these merchandise: If your organization is already utilizing a cloud supplier like AWS, Google Cloud Platform, or Azure it could be a good suggestion to undertake their pocket book answer, as accessing your organization’s infrastructure will possible be simpler and appear much less dangerous.
neptune.ai interactive dashboards assist ML groups to collaborate and share experiment outcomes with stakeholders throughout the corporate.
Right here’s an instance of how Neptune helped the ML group at Respo.Imaginative and prescient protected time by sharing leads to a typical setting.
I just like the dashboards as a result of we want a number of metrics, so that you code the dashboard as soon as, have these kinds, and simply see them on one display screen. Then, another particular person can view the identical factor, in order that’s fairly good.
Łukasz Grad, Chief Knowledge Scientist at ReSpo.Imaginative and prescient
Embracing efficient Jupyter pocket book practices
On this article, we’ve mentioned greatest practices and recommendation for optimizing the utility of Jupyter notebooks.
A very powerful takeaway:
At all times method making a pocket book with the supposed viewers and remaining goal in thoughts. In that approach, you know the way a lot focus to placed on the completely different dimensions of the pocket book (code, evaluation, govt abstract, and many others).
All in all, I encourage information scientists to make use of Jupyter notebooks, however completely for answering exploratory questions and reporting functions.
Manufacturing artefacts corresponding to fashions, datasets, or hyperparameters shouldn’t hint again to notebooks. They need to have their origin in manufacturing programs which can be reproducible and re-runnable. For instance, SageMaker Pipelines or Airflow DAGs which can be well-maintained and completely examined.
These final ideas about traceability, reproducibility, and lineage would be the place to begin for the subsequent article in my collection on Software program Patterns in Knowledge Science and ML Engineering, which can give attention to how you can uplevel your ETL abilities. Whereas typically ignored by information scientists, I consider mastering ETL is core and significant to ensure the success of any machine studying mission.