reflex-dev / reflex

🕸️ Web apps in pure Python 🐍
https://reflex.dev
Apache License 2.0
20.46k stars 1.18k forks source link

allow for 'go.Figure | None' annotation in State #4426

Closed Lendemor closed 3 days ago

Lendemor commented 5 days ago
import reflex as rx
import plotly.graph_objects as go

app = rx.App()

class State(rx.State):
    figure: go.Figure | None = None

    def create_figure(self):
        self.figure = go.Figure(
            data=[go.Bar(x=[1, 2, 3], y=[1, 3, 2])],
            layout=go.Layout(
                title=go.layout.Title(text="A Figure Specified By A Graph Object")
            ),
        )

@rx.page("/")
def index() -> rx.Component:
    return rx.cond(State.figure, rx.plotly(data=State.figure)), rx.button(
        "Init & Show Figure", on_click=State.create_figure
    )
adhami3310 commented 5 days ago

note, this barely works just because str({}) in python results in a valid js expression (when used in certain places). Ideally in the future, one should try to use rx.Var.create({})