I am trying to use threads in an MQTT connection (on_connect and on_message). I was trying to use this solution to be able to use streamlit inside the threads (as seen in Live plot from a thread - #12 by hcoohb). But I get the following error:
AttributeError: module ‘streamlit’ has no attribute ‘ReportThread’
Is it due to the new version of streamlit ? I have tried downgrading it to 1.3.0 and it does not change the error
def connect_mqtt():
KEY_CONTEXT = st.ReportThread.REPORT_CONTEXT_ATTR_NAME
main_context = getattr(current_thread(), KEY_CONTEXT)
def on_connect(client, userdata, flags, rc):
thread = current_thread()
if getattr(thread, KEY_CONTEXT, None) is None:
setattr(thread, KEY_CONTEXT, main_context)
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect to MQTT broker, return code %d\n", rc)
def on_message(client, userdata, msg):
thread = current_thread()
if getattr(thread, KEY_CONTEXT, None) is None:
setattr(thread, KEY_CONTEXT, main_context)
st.session_state["message"] = "Save message received from MQTT broker"
print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")
print("message received ", str(msg.payload.decode("utf-8")))
print("message topic=", msg.topic)
print("message qos=", msg.qos)
print("message retain flag=", msg.retain)
client = mqtt_client.Client(client_id)
client.username_pw_set(username, password)
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker, port)
return client
If you are having a similar issue, I would suggest starting a new thread, as commenting on a thread that was originally solved for the first user often gets ignored (because, people see it as a solved issue).
I wanted to take up this issue as I am also facing the same challenge.
When I upgrade to streamlit 1.4.0 or 1.5.0, we get an error: No Module named “streamlit.report_thread”.
I am using streamlit-analytics and was using SessionState old code. After removing SessionState old code, streamlit-analyticss seems to pose the problem while deploying.
Currently, I reverted back to 1.3.1 and it is working fine.
@whitphx We are facing the same issue when we upgraded to 1.9, we are also using streamlit analytics along with extracting sesion_state, not sure where should we make the change you suggested in your solution, could you provide some guidance