Skip to Content

Five challenges with Streamlit and their workarounds

Blog | September 7, 2022 | By Bindu Vucholla

Streamlit is a python based framework for building interactive web applications. We can effortlessly share, manage and deploy our apps, directly from Streamlit.

However, we faced some challenges in developing an end-to-end application using Streamlit and react. In this blog, we discuss those five challenges and how we overcame them.

Challenge 1: Communication between React and Streamlit:

Though the communication between react and Streamlit is bi-directional, there are some gaps which need to be addressed.

Let’s assume that we have a form to submit and pop-up in react. Form on submit sends data to python for database insertion & pop-up shows the success/fail response in iframe.

Here, How will the front-end receive database insert status(success/fail) from python? 

Workaround:

Render react form without popup. Display success/fail response using Streamlit API element st.success(‘Success’). 

Render only that specific react component which doesn’t need interaction with backend responses. Rest of the modules developed using the Streamlit elements. Refer Figure 1

Figure 1: Render react component

Note:

Tried Calling Streamlit component based on certain if else condition but it is re-rendering iframe and resetting the react current state. 

Challenge 2: Rendering multiple react components:

As we know that, react components are rendered in Streamlit as an iframe. Suppose when we have a multi-page Streamlit app and need to render multiple react components. i.e., each page should render a different react component. Let’s see how we can render multiple react components in Streamlit.

Workaround: 

In Streamlit, we should pass component name to render as args while declaring.

login = components.declare_component(“my_component”, url=”http://localhost:3001”)  login (component ="login")

Whereas in front-end, instead of rendering all the components, we should render component which is being received as args as like mentioned below:

(props.args.component=== 'login' ? <Login  props={props.args} /> : <Component1  props={props.args} /> )

Figure 2: Rendering Multiple React Components

Challenge 3: Resetting the values assigned to Streamlit variables while re-rendering the component:

Data sent to python using the method setComponentValue() is assigned to some variable in Streamlit.

Once the data is stored in that variable, it will never reset it to default, and then it will create a problem where the data are getting populated from the front-end are being inserted into a file/database.  

So, it inserts the data each time repeatedly, while rendering the iframe.

Workaround:  

In the front-end, once it loads, by default we can pass data as 0 (Zero) using the method streamlit.setComponentValue().

When submit event triggers, then the actual form data will be passed to Streamlit. In Streamlit we can verify the received data and insert them accordingly.  This helps Streamlit not to use the older form data after every run.

Challenge 4: Page Redirect in Streamlit:

After a successful login in any Streamlit app, user should be able to redirect to the landing page. However, there is no redirect method/API Elements available in Streamlit.  

Workaround: 

In multi-page Streamlit app, we can combine the login functionality code with main page code. Display either login form or landing page content based on the authentication status in session storage. 

Challenge 5: Hyperlink in Streamlit:

Hyperlink in Streamlit app opens the page in a new tab. There is no option to open hyperlink source in the same tab. 

Workaround

We can use the below specified html tags to overcome this Hyperlink issue: 

st.markdown('<a href="/" target="_self">Hyperlink </a>', unsafe_allow_html=True)
author image
About the Author
Software Engineer with over 4 years experience in Web Application development.
Bindu Vucholla | Software Engineer | USEReady
Back to top