I can not plot a simple graph

Hey everyone,
I am trying to plot a simple graph in a completely new app on Anvil. It renders the graph itself but not the bar charts. The axis is visible but the bars are missing.

What I’ve tried and what’s not working:
I have tried the most basic code for tests. In my previous app I could create graphs but for some reason now even the most basic graphs are not rendering. Probably I am missing something big time, so if you could help me would be great!

This is the code for the test app even this is not working.


from ._anvil_designer import Form1Template
from anvil import *
import plotly.graph_objects as go

class Form1(Form1Template):
def init(self, **properties):

    self.init_components(**properties)

    self.plot_test_graph()

def plot_test_graph(self):

    categories = ["Category A", "Category B", "Category C"]
    values = [10, 20, 30]


    fig = go.Figure(
        data=[
            go.Bar(x=categories, y=values, name="Test Data", marker_color="blue")
        ],
        layout=go.Layout(
            title="Test Bar Chart",
            xaxis_title="Categories",
            yaxis_title="Values",
            template="plotly_white"
        )
    )


    self.plot_1.data = fig

I have the plot component in and it is named as “plot_1”
I dont get any error either.

The plot code looks right at first glance, but I don’t see anywhere where the figure is assigned to a plot in the client. Something like:

self.plot_1.figure = fig

Also it’s good practice to add **event_args to the class function like this:

def plot_test_graph(self, **event_args):

That ** does a lot of work unpacking keyword arguments as things fly around so things work as you might want them to.

Hi,

Thanks for the corrections, you are completely right.
It works!

Thanks again that you took the time to answer!

1 Like

No problem, glad it worked!