Plotly Studio to Cloud: how to install new dependencies

Plotly Studio is great and the capability of deploying your apps to Plotly Cloud with just one click is even better! But sometimes you want to use a specific python library in your Plotly Studio app and since the requirements.txt is not exposed it makes it tricky to deploy to Plotly Cloud.

So, how do you install specific packages from your Studio app in Cloud?

Easy! Just add this to your code:

import sys
import subprocess
import ensurepip

ensurepip.bootstrap()
subprocess.check_call([sys.executable, "-m", "pip", "install", "<your-package>"])

But since not all Plotly Studio users are confident about modifying the code, I have found a way to make it possible with prompts. Just add this to your chart outline:

IMPORTANT: import ensurepip and run ensurepip.bootstrap()
IMPORTANT: Install with subprocess

Example:

I wanted to install the library scikit-learn to create a Random Forest classifier and assess it’s accuracy.

First I took this dataset from Kaggle: Telco Customer Churn. Then I removed all the charts since I wasn’t interested in exploring the data. And finally I added a new chart with this outline:

Random Forest Classifier

Use scikit-learn. Do a train-test split. Run random forest classification targeting only Churn. Display accuracy of classifier.

IMPORTANT: import ensurepip and run ensurepip.bootstrap()
IMPORTANT: Install scikit-learn with subprocess

And voilà! I got my chart with 4 subplots that allow me to assess the accuracy of my ML model:

Plotly Studio was smart enough to handle exceptions installing the library and not adding the installation in the callback. This way it only runs once: the first run after deployment.

You can check out the resulting app here: https://telco-churn-analysis.plotly.app/

4 Likes