Skip to content

Commit 557c299

Browse files
Update pyplot documentation to match latest reflex-graphing implementation (#1474)
* Update pyplot documentation to match latest reflex-graphing implementation - Replace outdated state management with @rx.var computed variables - Add fig_light and fig_dark computed vars with proper plt.close(fig) calls - Replace on_mount pattern with rx.color_mode_cond for theme switching - Add missing Figure import for proper type hints - Ensure interactive features (randomize button, slider) work in both light and dark modes Co-Authored-By: Alek <[email protected]> * Remove redundant plt.close() calls from computed variables The create_plot function already closes the figure, so additional plt.close() calls in the @rx.var methods were redundant and could cause issues. This addresses the greptile-apps bot feedback. Co-Authored-By: Alek <[email protected]> * Update reflex-pyplot version constraint to >= 0.2.0 - Changed from pinned version 0.1.3 to >= 0.2.0 for compatibility with latest pyplot features - Regenerated uv.lock file to reflect the version update - Ensures compatibility with @rx.var computed variables used in updated documentation Co-Authored-By: Alek <[email protected]> * Fix reflex-pyplot import statements for version 0.2.0 - Update import from 'reflex_pyplot' to 'custom_components.reflex_pyplot' - Fixes ModuleNotFoundError caused by module structure change in reflex-pyplot 0.2.0 - Updates all 4 locations: docs/__init__.py and 3 code blocks in pyplot.md Co-Authored-By: Alek <[email protected]> * Revert to simplified reflex-pyplot imports for version 0.2.1 - Update pyproject.toml to require reflex-pyplot >= 0.2.1 - Revert import statements from custom_components.reflex_pyplot back to reflex_pyplot - Update all 3 code blocks in pyplot documentation - Regenerate uv.lock with updated dependency Co-Authored-By: Alek <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Alek <[email protected]>
1 parent fe34aa9 commit 557c299

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

docs/library/graphing/other-charts/pyplot.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ from reflex_pyplot import pyplot
99
import numpy as np
1010
import random
1111
import matplotlib.pyplot as plt
12+
from matplotlib.figure import Figure
1213
from reflex.style import toggle_color_mode
1314
```
1415

@@ -97,36 +98,33 @@ def create_plot(theme: str, plot_data: tuple, scale: list):
9798

9899
class PyplotState(rx.State):
99100
num_points: int = 100
100-
plot_data: tuple
101-
scale: list
102-
fig: plt.Figure = plt.Figure()
101+
plot_data: tuple = tuple(np.random.rand(2, 100) for _ in range(3))
102+
scale: list = [random.uniform(0, 100) for _ in range(100)]
103103

104-
@rx.event
105104
def randomize(self):
106105
self.plot_data = tuple(np.random.rand(2, self.num_points) for _ in range(3))
107106
self.scale = [random.uniform(0, 100) for _ in range(self.num_points)]
108107

109-
@rx.event
110108
def set_num_points(self, num_points: list[int]):
111109
self.num_points = num_points[0]
112110
self.randomize()
113111

114-
@rx.event
115-
def create_fig(self, theme: Literal["light", "dark"]):
116-
self.plot_data = tuple(np.random.rand(2, 100) for _ in range(3))
117-
self.scale = [random.uniform(0, 100) for _ in range(100)]
118-
self.fig = create_plot(
119-
theme, self.plot_data, self.scale
120-
)
112+
@rx.var
113+
def fig_light(self) -> Figure:
114+
fig = create_plot("light", self.plot_data, self.scale)
115+
return fig
116+
117+
@rx.var
118+
def fig_dark(self) -> Figure:
119+
fig = create_plot("dark", self.plot_data, self.scale)
120+
return fig
121121

122122
def pyplot_example():
123123
return rx.vstack(
124124
rx.card(
125-
pyplot(
126-
PyplotState.fig,
127-
width="100%",
128-
height="100%",
129-
on_mount=rx.color_mode_cond(PyplotState.create_fig("light"), PyplotState.create_fig("dark")),
125+
rx.color_mode_cond(
126+
pyplot(PyplotState.fig_light, width="100%", height="100%"),
127+
pyplot(PyplotState.fig_dark, width="100%", height="100%"),
130128
),
131129
rx.vstack(
132130
rx.hstack(

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ dependencies = [
2020
"mistletoe>=1.2.1",
2121
"reflex-image-zoom>=0.0.2",
2222
"replicate==1.0.6",
23-
"reflex-pyplot==0.1.3",
23+
"reflex-pyplot>=0.2.1",
2424
"reflex-enterprise==0.3.1a1",
2525
"requests>=2.32.3",
2626
"posthog==4.0.1",

uv.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)