I’m trying to create a little proof demo in where a fastapi endpoint will take string (sent by another docker container) and write that string to streamlit.
The project structure currently consists of 2 containers.
container 1 will have the ui (streamlit) and a parallel instance of fastapi that will receive information and write it to streamlit.
container 2 will have the engine (logic, inferencing and other stuff that executes the task) and another instance of fastapi that listens for commands for task inputs.
What I am doing with the project is when the user provides the input on the task they want done, those instructions are sent to container 2. The task takes a while and continously generates bits of output that gets sent back to container 1 to be outputted to the UI.
What I’m struggling with is to get the fastapi instance in container 1 to write data it receives into the UI. Wonder how other people do it.
I managed to dummy up a websocket that managed to get it a bit closer.
Problem is because streamlit reruns the file whenever a widget is pressed, whenever button ‘AD’ is pressed the websocket is lost and it won’t work. However if we don’t use button ‘AD’ and go the the url “http://app:8050/call-api” directly in the browser I can see the message being written on streamlit. However I’m stuck trying to get this to work and everything I’ve tried doesn’t work and storing async into session states seems to be a no go.
import streamlit as st
import asyncio
import websockets
import requests
WEBSOCKET_URL = "ws://app:8050/ws"
async def listen_to_websocket():
async with websockets.connect(WEBSOCKET_URL) as websocket:
while True:
message = await websocket.recv()
if message:
st.write(message)
async def main():
await listen_to_websocket()
# Streamlit app
st.title("Streamlit App")
if st.button("start"):
asyncio.run(main())
if st.button("AD"):
response = requests.get("http://app:8050/call-api")
fastapi
import uvicorn
from typing import Union
import requests
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
import asyncio
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
)
connections = set()
@app.get("/call-api")
async def call_api():
# Notify all connected WebSocket clients
for connection in connections:
await connection.send_text("TEST!!!!")
return {"message": "API called"}
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
connections.add(websocket)
try:
while True:
# Keep the connection alive
await websocket.receive_text()
except Exception as e:
print(f"WebSocket error: {e}")
finally:
connections.remove(websocket)
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.