A Quick Way to Prototype Your Data Science Projects on the Fly



Original Source Here

A Quick Way to Prototype Your Data Science Projects on the Fly

Transform your Jupyter notebooks into interactive dashboards

Quickly create prototypes for data science projects by converting Jupyter notebooks into interactive dashboards — .Photo by Rodolfo Clix from Pexels

Your data science project would come along great when you have a lean technology stack.

Yes, the smaller the stack, the better the project!

Jupyter notebooks are inseparable from data scientists’ day-to-day work. Also, most data science projects need an interactive dashboard as well.

What if you could convert your notebooks into feature-rich dashboards 💡.

You can, now!

Typically, developing a dashboard requires HTML, JavaScript, and CSS knowledge. But tools such as Streamlit and Dash have made it possible to create dashboards without them.

Yet, you still have to convert your notebooks into a functional app manually. This involves a lot of copying and pasting.

But with mercury, you could instantly turn your Jupyter notebooks into an interactive dashboard, an online slideshow, or a web service. You can install the library from PyPI package repository.

pip install mljar-mercury

How to convert your notebooks into functional dashboards?

All it takes is a YAML configuration!

Yes, on the top of your notebook, you need to define a RAW cell with a YAML configuration. You’ll have all the dashboard properties, widgets, and behaviors in this.

The widget names will have an automatic mapping with the variables in your notebook. Yet, you should define the variables at a global level.

That makes sense, right? One notebook turns into one dashboard. Its global variables are the ones we need to interact with. So define widgets for them.

So here’s an example to start with.

A hello world example of creating an interactive dashboard out of a Jupyter notebook. — Image by the author.

This notebook has a single widget — an input field to type in your name. Then our dashboard will say hi to you.

Note that the widget name under the param section of the YAML matches our variable name.

Let’s run this example. To start the dashboard server, execute the following command in a terminal.

mercury run dashboard.ipynb # if dashboard.ipynb is your notebook

This command will start a Django server and a celery process. If it’s the first time, Mercury will also run database migrations. But you don’t have to worry about them now.

A hello world example of converting Jupyter notebook into an interactive dashboard. — Image by the author.

Bingo! you’ve created a dashboard! No HTML! No JavaScript! No CSS!

Use more cool widgets and themes!

We used a single widget in the previous example. Yet, Mercury ships with a dozen cool ones like sliders, dropdowns, checkboxes, etc.

Each one has a slightly different configuration. For instance, the following code will display a range slider.

params:
range_variable:
input: range
label: Select the minimum and maximum
value: [45,50]
min: 30
max: 65

Note that we have given initial values through the value property.

This will render a range slider as below.

Range picker slider example in Mercury — Screenshot by the author.

Check out the widgets documentation for a complete list and their configurations.

Also, if you’re making your app an online slideshow, you could pick a theme. To make a presentation, you have to specify the output type as slides and the theme of your preference. Here’s an example.

output: slides
format:
theme: moon

You could pick one of the eleven themes available.

Securely share your dashboard with others

This is the most straightforward way I know to create dashboards. It’s the quickest way to prototype your ideas too.

Also, it’s a typical Django application. So, you can host it behind a proxy server (Nginx or Apache) like you’d host any other web application.

Also, if you start the server with an open host and port numbers, as shown below, you could let others in your local network access it in real-time.

mercury run dashboard.ipynb 0.0.0.0:8000

People in your network can access it with http://<Your local IP>:8000/.

A common concern when sharing apps with others is security. You may not want everyone to see the dashboard.

Mercury makes use of Django’s authentication system. You can create user accounts from the admin panel.

By default, all the dashboards we create are public. You can make them private by specifying share: private in your YAML file. This will make the dashboard available for the logged-in users.

However, authentication is a pro feature. Thus, share your dashboards with caution unless you have a commercial license.

Create an interactive machine learning dashboard

If you could control the global variables of a notebook, you could handle anything in it.

If you’re using notebooks to build machine learning models (who’s not? 😏) you could do a lot more with this interactive dashboard.

You can tweak the hyperparameters, alternate between datasets, and much more.

To illustrate this, we do a trivial K-Means clustering with interactive cluster number selection. I’ve used this example in several previous posts. I like K-means for its simplicity.

The following notebook will create a dashboard to adjust cluster numbers in real-time.

An interactive dashboard created out of Jupyter notebook to adjust machine learning parameters— Screenshot by the author.

You can adjust the slider to change clusters and hit the “Run” button for re-rendering.

Share offline reports as PDFs and static pages

In most cases, you’ll have to use dashboards as reporting tools. You need a powerful export functionality.

Mercury comes with a free inbuilt export function. You can download your reports as PDFs or static HTML files. Simply adjust the widgets, run the report, and click the download button.

Next time you want to submit a report, you could use this tool instead of manually copy-pasting your matplotlib charts.

Final thoughts

I’ve mentioned this several times.

Developing apps is not a data scientist’s superpower. Building ML models are.

But almost all the work of a data scientist ends up with an app or a dashboard. Also, without such a tool, it’s impossible to showcase or illustrate the great work.

Over the past few years, many technologies have emerged to bridge the gap. Today, data scientists hardly need HTML, CSS, or JavaScript skills to build a production-ready application. You can use tools such as Streamlit or Dash to build an app in minutes.

Yet, Mercury makes it even easier to prototype data science projects on the fly. Building a sharable app without leaving the notebook environment is truly a gamechanger.

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: