OpenBB-finance/OpenBB · warning · ValueError

Error: No chart has been created. Please create a chart firs

Error message

Error: No chart has been created. Please create a chart first.

What it means

Charting.toggle_chart_style() flips the current chart between plotly light/dark templates, but requires an existing chart figure stored on obbject.chart. If no chart was created yet (obbject.chart has no 'fig' attribute), there is nothing to restyle and this ValueError is raised.

Source

Thrown at openbb_platform/obbject_extensions/charting/openbb_charting/charting.py:671

                    fig=fig, content=content, format=self._format
                )
                if render:
                    return fig.show(**kwargs)  # type: ignore
            except Exception as e:  # pylint: disable=W0718
                raise RuntimeError(
                    "Failed to automatically create a generic chart with the data provided."
                ) from e

    def _set_chart_style(self, figure: "Figure"):
        """Set the user preference for light or dark mode."""
        return figure

    def toggle_chart_style(self):
        """Toggle the chart style between light and dark mode."""
        import plotly.io as pio  # pylint: disable=import-outside-toplevel

        if not hasattr(self._obbject.chart, "fig"):
            raise ValueError(
                "Error: No chart has been created. Please create a chart first."
            )
        current = self._charting_settings.chart_style
        new = "light" if current == "dark" else "dark"
        self._charting_settings.chart_style = new
        template_name = "plotly_white" if new == "light" else "plotly_dark"
        figure = self._obbject.chart.fig  # type: ignore[union-attr]
        figure.update_layout(template=pio.templates[template_name])  # type: ignore[union-attr]
        self._obbject.chart.fig = figure  # type: ignore[union-attr]
        self._obbject.chart.content = figure.show(  # type: ignore[union-attr]
            external=True
        ).to_plotly_json()  # type: ignore[union-attr]

    @staticmethod
    def _convert_to_string(x):
        """Sanitize the data for the table."""
        # pylint: disable=import-outside-toplevel
        from numpy import isnan

View on GitHub (pinned to 3e071fcc2c)

Solutions

  1. Create a chart first: res = obb.equity.price.historical('AAPL'); res.charting(); res.charting.toggle_chart_style()
  2. Guard with hasattr(res.chart, 'fig') before toggling
  3. Set the preferred style in user settings/charting config up front so toggling is unnecessary

Example fix

# before
res = obb.equity.price.historical('AAPL')
res.charting.toggle_chart_style()

# after
res = obb.equity.price.historical('AAPL')
res.charting()
res.charting.toggle_chart_style()
Defensive patterns

Strategy: validation

Validate before calling

if not hasattr(res.chart, 'fig'):
    res.charting()  # create the chart first
res.charting.toggle_chart_style()

Type guard

def chart_exists(res) -> bool:
    return hasattr(res.chart, 'fig')

Try / catch

try:
    res.charting.toggle_chart_style()
except ValueError as e:
    if 'No chart has been created' in str(e):
        res.charting(); res.charting.toggle_chart_style()

Prevention

When it happens

Trigger: Calling obbject.charting.toggle_chart_style() before any .charting() or to_chart() call; chart creation previously failed so obbject.chart was never set; calling on a freshly built OBBject with manual results.

Common situations: User toggles dark mode in a notebook/script before generating a chart; charting threw earlier and the error was swallowed; interactive sessions where the chart cell ran out of order.

Related errors


AI-assisted analysis of OpenBB-finance/OpenBB@3e071fcc2c (2026-08-14). Data as JSON: /api/errors/d907b7b694a3e14c. Report an issue: GitHub.