cancel
Showing results for 
Search instead for 
Did you mean: 
Machine Learning
Dive into the world of machine learning on the Databricks platform. Explore discussions on algorithms, model training, deployment, and more. Connect with ML enthusiasts and experts.
cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with ipywidgets and plotly on Databricks

moseb
New Contributor II

Hi everyone, 

I am encountering a problem when using ipywidgets with plotly on Databricks. I am trying to pass interactive arguments to a function and then plot with plotly. 

When I do the following

def f(m, b) :
    plt.figure(2)
    x = np.linspace(-10, 10, num=1000)
    plt.plot(x, m * x + b)
    plt.ylim(-5, 5)
    plt.show()

interactive_plot = interactive(f, m=(-2.0, 2.0), b=(-3, 3, 0.5))
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot
 
I obtain the expected result. The following code instead
 
def f(m, b) :
    x = np.linspace(-10, 10, num=1000)
    y = m * x + b
    df = pd.DataFrame({'x': x, 'y': y})
    fig = px.line(df, x='x', y='y')
    fig.update_yaxes(range=[-5, 5])
    fig.show()

interactive_plot = interactive(f, m=(-2.0, 2.0), b=(-3, 3, 0.5))
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot
 
causes the message Latest error: Uncaught ReferenceError: Plotly is not defined.
 
I am importing plotly.express and the same code runs smoothly locally on my computer.
 
What could be the problem? Thank you in advance!
2 REPLIES 2

TheRealOliver
Contributor

It would help a lot if you attach a notebook or copy the full code that demonstrates the problem. This would make it easy for someone to copy/paste, run, and troubleshoot it and you are more likely to get effective help that way 😉

Also, since your Databricks notebook runs on some kind of Databricks compute, it might be useful to mention what are the parameters of that compute. If it is not serverless, you could include a screenshot of the cluster configuration. Things that could matter in troubleshooting are the access mode and whether it's a single user cluster and what version of Databricks does it run.

I hope this helps!

moseb
New Contributor II

Thanks for the suggestion! You're absolutely right. The code was already all in my message, but I can make it easier to copy-paste (and add the imports):

from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np

def f(m, b):
    plt.figure(2)
    x = np.linspace(-10, 10, num=1000)
    plt.plot(x, m * x + b)
    plt.ylim(-5, 5)
    plt.show()

interactive_plot = interactive(f, m=(-2.0, 2.0), b=(-3, 3, 0.5))
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot


I'll also include details about the Databricks compute environment in a screenshot.

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now