Hi Streamlit community,
As English isn’t my native language, I didn’t managed to find a topic discussing about this already… I try to create a custom component with React/MUI/Typescript technologies. Everything is working well but I ran into a little issue of comprehension I guess : my custom component doesn’t initialize its default value in the session state variable called by its key. I hope you can help me understand what’s going on here.
Hereunder, I show you the result of a simple reproduction of a MUI Select component. I wrote a script that instantiates one custom component, then writes its output and finally writes the session state of the application.
init.py file :
import streamlit.components.v1 as components
_select_image = components.declare_component(
"streamlit_select_image",
url="http://localhost:3001",
)
def st_select_image(options, label="", key=None):
image = _select_image(label=label, options=options, key=key, default=options[0])
return image
Custom React/MUI component :
import {
Streamlit,
withStreamlitConnection,
} from "streamlit-component-lib"
import React, { ComponentProps, useEffect, useState } from "react"
import MenuItem from '@mui/material/MenuItem';
import Select, { SelectChangeEvent } from "@mui/material/Select"
import { Box, FormControl, InputLabel } from "@mui/material"
const SelectImage = (props: ComponentProps<any>) => {
const options = props.args["options"];
const [value, setValue] = useState(options[0]);
useEffect(() => Streamlit.setFrameHeight(150));
function handleChange(event: SelectChangeEvent) {
Streamlit.setComponentValue(event.target.value as string);
setValue(event.target.value);
}
return (
<Box sx={{ minWidth: 50 , marginTop: 2}}>
<FormControl fullWidth>
<InputLabel id="image-select-label">{props.args["label"]}</InputLabel>
<Select
labelId="image-select-label"
id="image-select"
value={value}
label="Image"
onChange={handleChange}
>
{options.map((data: string) => (
<MenuItem value={data}>{data}</MenuItem>
))}
</Select>
</FormControl>
</Box>
);
}
export default withStreamlitConnection(SelectImage)
Hereunder, you can see that the output of my custom component is as expected the first option on its list. However, the session state variable attached to its key is NULL.
Once I’ve selected another option on its list, it actualize the session state variable, as you can see on the following screenshot.
Am I doing something wrong at the initialization of the component ? Or is it impossible to initialize the session state variable connected to the instance of a custom component ?
I hope my question was clear, please, let me know if you want me to rephrase it.
Thank you for your help,
Happy New Year everyone !
Victor