Fixing Plotly Chart Deprecation Warning In Streamlit
Have you ever been happily coding away in Streamlit, building a fantastic interactive dashboard, and then BAM! A deprecation warning pops up, seemingly out of nowhere? If you've recently encountered a warning related to st.plotly_chart and kwargs even when you're just trying to set the width or height, you're not alone! Many Streamlit users have hit this little snag, and it can be a bit confusing. This article is here to clear the air, explain what's happening, and show you how to navigate it smoothly.
Understanding the st.plotly_chart Kwargs Warning
The core of the issue lies in how Streamlit handles arguments passed to its components, specifically st.plotly_chart. When you use this function to display a Plotly chart, you can customize its appearance and behavior. Historically, Streamlit allowed for flexible passing of arguments, sometimes referred to as kwargs (keyword arguments). However, as Streamlit evolves, it aims to provide clearer and more robust ways to configure its elements. A recent change introduced a deprecation warning for variable kwargs in st.plotly_chart, suggesting the use of the config argument for Plotly-specific configurations instead. This is a good thing in the long run, promoting better practices and making the API more predictable. The problem arises when this warning is triggered even for standard, documented parameters like width='content' or height='auto', where you're not intending to pass arbitrary keyword arguments.
Initial Intent vs. Actual Implementation
It seems that in certain versions of Streamlit (specifically mentioned as 1.50.0 in the user's report), the internal logic that checks for these kwargs might be a bit overzealous. When you provide parameters like width='content' or height='auto' directly to st.plotly_chart, Streamlit is supposed to recognize these as legitimate, documented parameters. However, the warning: "Variable keyword arguments for st.plotly_chart have been deprecated and will be removed in a future release. Use the config argument instead to specify Plotly configuration options." is showing up. This suggests that internally, Streamlit might be processing these arguments in a way that triggers the kwargs detection, even though they are standard and expected parameters.
This is particularly frustrating because width and height are fundamental attributes for controlling how your charts fit within your Streamlit application's layout. Forcing users to figure out how to pass these through the config argument when they were previously straightforward can feel like a step backward, even if the overall goal is improved API design. The goal of this warning was to steer users away from passing unintended or Plotly-specific arguments directly into st.plotly_chart that should go into the config dictionary. However, it's inadvertently catching standard layout parameters.
Why this is a Regression:
As the user rightly pointed out, this is a regression. Previously, using st.plotly_chart(fig, width='content') worked perfectly fine without any warnings. The fact that it now triggers a deprecation warning, even though the parameters are standard and documented, indicates a change in behavior that breaks existing, functional code. This can lead to confusion and a perceived instability in the library, especially for developers who rely on these parameters for responsive design.
The config Argument:
For clarity, the config argument in st.plotly_chart is intended for passing Plotly.js configuration options. This includes things like responsive: True, displaylogo: False, or scrollZoom: True. These are settings that directly influence the behavior and appearance of the Plotly chart itself, as rendered by Plotly.js. Parameters like width and height in st.plotly_chart are Streamlit-level controls for how the chart is embedded within the Streamlit app. While they affect the visual presentation, they operate at the Streamlit component level rather than the Plotly chart's internal rendering settings. The confusion arises because width and height can also be set within a Plotly figure's layout, but st.plotly_chart provides a direct way to control the container's size.
This specific warning points to an internal interpretation issue within Streamlit's st.plotly_chart function. It's important to distinguish between arguments Streamlit itself uses for component layout (like width, height, use_container_width) and arguments intended for the underlying plotting library (Plotly, in this case), which should be passed via config. The deprecation is aimed at the latter, but it's mistakenly flagging the former.
Reproducing the Issue: A Simple Code Example
To help illustrate the problem, let's look at the provided reproducible code example. It's straightforward and clearly demonstrates when the warning appears:
import plotly.graph_objects as go
import streamlit as st
# Create a simple Plotly figure
fig = go.Figure()
fig.add_trace(go.Barpolar(r=[1], theta=[0], width=[120]))
fig.update_layout(width=500, height=360) # These are figure layout settings
# Display the chart using st.plotly_chart with width='content'
st.plotly_chart(fig, width='content')
Breaking Down the Code:
import plotly.graph_objects as go: This line imports the necessary module from the Plotly library to create figures and traces.import streamlit as st: This imports the Streamlit library, which we're using to build our web application.fig = go.Figure(): We initialize an empty Plotly figure object. This will hold our chart.fig.add_trace(go.Barpolar(r=[1], theta=[0], width=[120])): Here, we add aBarpolartrace to our figure. Note thatwidth=[120]here is a parameter specific to theBarpolartrace itself, controlling the width of the bars in degrees. This is distinct from the container width we'll set later.fig.update_layout(width=500, height=360): This line sets the intrinsic width and height of the Plotly figure in pixels. These are properties of the Plotly figure object.st.plotly_chart(fig, width='content'): This is the crucial line. We're telling Streamlit to display ourfig. Thewidth='content'argument is a Streamlit-specific parameter designed to make the chart take up the full available width of its container in the Streamlit app. This is the argument that triggers the deprecation warning.
Steps to Trigger the Warning:
- Make sure you have Streamlit version 1.50.0 (or a version where this bug exists) and Plotly installed.
- Save the code above as a Python file (e.g.,
app.py). - Run the file using
streamlit run app.pyin your terminal.
When the Streamlit app loads, you will see your Barpolar chart rendered. However, in your terminal or in the Streamlit output (depending on Streamlit's logging configuration), you'll notice the deprecation warning message regarding kwargs.
Why this specific setup?
The code demonstrates the exact scenario described: using a standard, documented parameter (width='content') for st.plotly_chart. It doesn't involve passing any other unusual or undocumented arguments. The fig.update_layout(width=500, height=360) is included to show that the Plotly figure itself has dimensions, but the warning is triggered by the Streamlit width parameter controlling the container.
This example is effective because it isolates the issue to the st.plotly_chart function's handling of its own parameters, rather than a misunderstanding of Plotly's internal configurations. It highlights that the warning system is incorrectly flagging a valid use case, which is the definition of a regression bug.
Expected vs. Current Behavior
Let's clarify what should happen versus what is happening, based on the user's report.
Expected Behavior
Ideally, when you use st.plotly_chart with parameters that are part of its documented API, such as width, height, or use_container_width, it should function without issuing deprecation warnings. The intention behind these parameters is to control how the chart is displayed within the Streamlit application's layout.
Specifically, when using width='content', the expectation is that the Plotly chart will automatically resize to fill the available horizontal space in its parent container. Similarly, height='auto' would adjust the height based on the content. The st.plotly_chart function is designed to manage these layout aspects directly. Therefore, passing these parameters should be considered a standard operation, not an indication of using deprecated variable keyword arguments.
The deprecation warning is intended for situations where a user might pass arbitrary Plotly-specific configurations directly as keyword arguments to st.plotly_chart, rather than using the dedicated config dictionary. For example, passing responsive=True directly might trigger a warning, encouraging the user to instead use st.plotly_chart(fig, config={'responsive': True}). The expected behavior is that standard layout parameters are not caught by this warning.
Current Behavior
As reported, the current behavior is that even when using documented parameters like width='content', a deprecation warning is issued:
Variable keyword arguments for st.plotly_chart have been deprecated and will be removed in a future release. Use the config argument instead to specify Plotly configuration options.
This warning appears to be triggered by an internal check within st.plotly_chart. The code snippet provided in the issue suggests a logic like this:
if kwargs:
show_deprecation_warning(
"Variable keyword arguments for st.plotly_chart have been "
"deprecated and will be removed in a future release. Use the "
"config argument instead to specify Plotly configuration "
"options."
)
It seems that the way width='content' (or similar parameters) is handled internally causes it to be included in whatever kwargs variable this check is monitoring. This means that Streamlit is incorrectly identifying a standard, intended use of its API as an instance of using deprecated variable keyword arguments.
This inconsistency is what makes it a regression. Users who were previously able to set chart widths and heights without issue are now confronted with a warning that suggests they are misusing the API, when in fact, they are using it as documented.
The Disconnect:
The disconnect lies in the interpretation of kwargs. The developer's intent when writing st.plotly_chart(fig, width='content') is to control the Streamlit container's size for the chart. The warning, however, seems to be preemptively flagging any keyword arguments that aren't explicitly defined as top-level parameters of st.plotly_chart itself, or perhaps arguments that could also be interpreted as Plotly configurations if passed differently. The ideal solution would be for Streamlit to correctly differentiate between its own layout arguments and Plotly's internal configuration options.
Is This a Regression?
Yes, this is definitively a regression. A regression in software occurs when a change introduces a new bug or causes previously working functionality to stop working as expected. In this case, the behavior described fits the definition of a regression perfectly.
What worked before:
In earlier versions of Streamlit, developers could comfortably use parameters like width='content' and height='auto' directly within the st.plotly_chart() function call without encountering any warnings. This was a standard and documented way to control how their Plotly charts were displayed within the Streamlit interface. For instance, setting width='content' was a common method to ensure charts were responsive and utilized the available screen space effectively. The API was clear: you passed chart objects and layout/display parameters directly to st.plotly_chart.
What changed:
With updates to Streamlit, particularly around how keyword arguments are handled and how Plotly configurations are managed, the internal logic for detecting