Component example: Rich data
This is a component that receives various data types from Python, including an Arrow-serializable dataframe, a JSON-serializable dictionary, and a Base64-encoded image.
Key concepts demonstrated
This component demonstrates the following concepts:
- Passing data from Python via the
dataparameter and accessing it in JavaScript - Automatic dataframe and JSON serialization
- Passing an image as a Base64-encoded string
- Using a placeholder in the component's HTML and dynamically updating it with received data
Complete code
How it works
The data parameter
When mounting a component, the data parameter passes information from Python to JavaScript. Streamlit automatically serializes the data:
- DataFrames are converted to Apache Arrow format if passed directly to
dataor included as a value in a dictionary. - Dictionaries, lists, strings, numbers, booleans are JSON-serialized.
- Bytes can be passed directly to
data, but can't be passed as a value in a dictionary.
Accessing data in JavaScript
The data property is available in the component function's argument object:
export default function ({ data, parentElement }) {
// data contains everything passed from Python
const df = data.df;
const userInfo = data.user_info;
}
Note
DataFrames arrive as Arrow-formatted data on the frontend. In this simple example, they're converted to a string for display. For more sophisticated handling, you can use libraries like Apache Arrow JS to parse and manipulate the data.
Dynamic updates
When data changes between reruns, your component's JavaScript function is called again with the new data. This enables reactive components that update based on Python state. For an example of a bidirectional reactive component, see the Text input component example.
Still have questions?
Our forums are full of helpful information and Streamlit experts.