I’m a junior developer working on a project where my colleague is using Streamlit to interact with a customized Llama model. I’m building a React front-end to allow users to query the model, but I’m having difficulty setting up bidirectional communication between React and Streamlit.
Here’s what I’ve tried so far (running on a local environment):
1.) Using a Middle Layer (Preferred Solution):
I set up a FastAPI server as a middle layer between React and Streamlit:
React → FastAPI: React sends user queries to FastAPI via POST requests.
FastAPI → Streamlit: FastAPI forwards the queries to Streamlit via the requests library.
Streamlit → FastAPI → React: Streamlit responds, and FastAPI relays the response back to React.
The Streamlit server includes an embedded FastAPI app that exposes a /query endpoint.
Despite configuring CORS and headers (to the best of my knowledge), I ran into consistent 403 Forbidden errors when FastAPI tried to make POST requests to Streamlit.
2.) Streamlit Components:
I used the streamlit-component-lib to create a custom React component. Despite React sending data using Streamlit.setComponentValue, the Streamlit app did not receive or process the data.
I’m not seeking specific debugging help but rather guidance on the best way to approach bidirectional communication between React and Streamlit from scratch. Is using a middle layer like FastAPI or Flask viable, and if so how specifically would this need to be set up, or is there a better pattern recommended by Streamlit for this use case?
If you need to build custom frontend components, Streamlit Components should be the way to go. May I ask what custom UI you are building for Streamlit? (You might have some easier options with existing custom components, either to use as is, or to modify.)
Streamlit Components can definitely be bidirectional. I’d point you to the docs or component templates if you don’t want to specifically discuss your own component.
To clarify, my project requires a React front-end and middle layer back-end for a few key reasons, including user authentication, which Streamlit doesn’t natively support. The React front-end will also allow us to extend our app’s functionality and UI beyond Streamlit’s built-in capabilities. The interaction we’re aiming for is:
The React front-end sends user queries to a Streamlit app (where a Llama model runs) and displays the model’s responses in our chatbot interface. The React app needs to send queries and receive responses dynamically which is why the bidirectional communication is needed.
With Streamlit components, my understanding is that they only extend Streamlit’s UI within its framework, but I’ve been trying to integrate a standalone React front-end in a way that enables seamless bidirectional communication with Streamlit. The best solution to me seems like using a middle layer (like Flask/FastAPI), would that be possible and how would I go about that?
As for your described architecture, I’m a little confused. Streamlit as a product is a front-end. Streamlit commands are commands to generate a displayed page for users. I’m not sure what role Streamlit would play if you want a 100% custom front-end.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.