I’m writing tests for my app which produces plotly charts. The app testing cheat sheet says that chart elements are not supported, but you can use AppTest.get() to inspect most of the unsupported elements.
Since AppTest.get("plotly_chart") gives me a list of UnknownElement(), I’m guessing this approach doesn’t work for plotly charts.
Am I right in assuming that plotly charts can’t be inspected for testing, or is there another way to somehow get to my charts?
It’s pretty ugly but you can extract the figure spec from the underlying proto attached to the UnknownElement you mentioned. We’re hoping to add a more convenient to use method of support soon.
Here’s an example test that will print out the contents of the figure spec as a dictionary, using the plotly_chart code from the docs example. The test always fails so you can see the printed output (it’s very long).
from streamlit.testing.v1 import AppTest
import json
import pprint
APP_CODE = """
import streamlit as st
import numpy as np
import plotly.figure_factory as ff
# Add histogram data
x1 = np.random.randn(200) - 2
x2 = np.random.randn(200)
x3 = np.random.randn(200) + 2
# Group data together
hist_data = [x1, x2, x3]
group_labels = ['Group 1', 'Group 2', 'Group 3']
# Create distplot with custom bin_size
fig = ff.create_distplot(
hist_data, group_labels, bin_size=[.1, .25, .5])
# Plot!
st.plotly_chart(fig, use_container_width=True)
"""
def test_chart():
at = AppTest.from_string(APP_CODE).run()
spec = json.loads(at.get("plotly_chart")[0].proto.figure.spec)
pprint.pprint(spec)
assert True == False
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.