# Live plot from a thread

**URL:** <https://discuss.streamlit.io/t/live-plot-from-a-thread/247>\
**Category:** Using Streamlit\
**Created:** [October 7, 2019, 4:51am UTC](https://discuss.streamlit.io/t/live-plot-from-a-thread/247 "2019-10-07T04:51:19Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/thiago/32/22_2.png) [@thiago](https://discuss.streamlit.io/u/thiago)\
**Post date:** [October 8, 2019, 3:48am UTC](https://discuss.streamlit.io/t/live-plot-from-a-thread/247/2 "2019-10-08T03:48:57Z")

</div>

Hi @robmarkcole

To use Streamlit outside the main thread you need to grab some special objects and use them to write to the app inside the thread. This depends on some obscure Streamlit internal details, so I created a little wrapper to make your life easier: [Hack to write to Streamlit app from another thread. See: https://discuss.streamlit.io/t/live-plot-from-a-thread/247 · GitHub](https://gist.github.com/tvst/fa33b9dcb58040cbcb0ea376146d4e8c)

How to use it:

```python
# This must be called from the main thread:
thread_safe_st = ThreadSafeSt()

def function_called_from_another_thread(arg1, arg2, st):
  st.main.markdown('arg1 is: %s' % arg1)
  st.sidebar.markdown('arg2 is: %s' % arg2)

# On another thread:
function_called_from_another_thread(10, 20, thread_safe_st)

```

* * *

> **Also I am not sure if using a `while True` to maintain the app is best practice?**

Since you’re using `mqtt`, you should be able to replace the `while True` with just `client.loop_forever()`. See relevant docs [here](https://pypi.org/project/paho-mqtt).

So I think the code would look like this in the end:  
_(Note: this is untested!)_

```python
import paho.mqtt.client as mqtt
import streamlit as st
import time
from thread_safe_st import ThreadSafeSt

MQTT_BROKER = 'localhost'

thread_safe_st = ThreadSafeSt()

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    # Note: st.write is not supported in thread_safe_st yet, but will be soon!
    # See https://github.com/streamlit/streamlit/pull/238
    thread_safe_st.markdown(
      f"Connected with result code {str(rc)} to MQTT broker on {MQTT_BROKER}")

def on_disconnect(client, userdata,rc=0):
    print("DisConnected result code "+str(rc))
    client.loop_stop()

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    thread_safe_st.text(str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_BROKER)
client.subscribe("streamlit")

client.loop_forever()

```

---

_[View the full topic](https://discuss.streamlit.io/t/live-plot-from-a-thread/247)._
