Reflex Cloud - Fast, secure & scalable hosting. One command to deploy.

Back to Blog

New Core Graphing Components

Using Reflex's new core graphing feature to build a live streaming graphing app.

Alek Petuskey

·

Image for blog post: New Core Graphing Components

With the release of Reflex v0.2.9 we've reworked the core graphing components from the ground up. The new components are more flexible and easier to use.

In this post we'll walk through the new components and show how to build a live streaming graphing app. This will also build on the Unlocking New Workflows with Background Tasks post, so if you haven't read that yet, check it out first.

Striking a Balance

With our new graphing components we are aiming to strike a balance between flexibility and ease of use. We want to make it easy to build a graph, but we also want to make it easy to customize the graph to your needs.

Check out our graphing docs for more details on the new components.

Livestreaming Example

In this example we will create a live streaming graph that updates every second with random data.

We start by defining some initial data for our chart to use as well as some imports we will use for the project:

Here uv stands for unique visitors, pv stands for page views, and amt stands for amount. They are arbitrary values that we will use to populate our graph.

Next we define a StreamingState class that will be used to store the data and update it with an event handler:

Here we define a stop_stream method that will stop the stream when called. We also define a start_stream method that will start the stream. We use the @rx.event(background=True) decorator to run the method in the background. This allows us to update the data without blocking the UI.

Remember to use async with self: when updating the state in a background task.

Finally we will define our UI using Reflex's new graphing components. We pass the data from our StreamingState class to the area_chart component and reference the data key we want to use in area component. We also add a buttons to start and stop the stream.

The result is a live updating graph that looks like this:

Extra Example

We can add extra area components to our chart to show the uv and amt data as well. We can also add a graphing_tooltip an cartesian_grid component to show the data when we hover over the chart.

Keep in mind the child coming first will be display in the back so the order of the area components matters.

Conclusion

We hope you enjoy the new graphing components. We are excited to see what you build with them. If you have any questions or feedback, please reach out to us on Discord.

-- Reflex Team

More Posts

Newsletter

Get the latest updates and news about Reflex

$ pip install reflex

$ reflex init

$ reflex run

Need help? Learn how to use Reflex.

Built with Reflex