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
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.