Bad Message Format : 'setIn' cannot be called on an ElementNode

Hello Everyone.
I am trying to build a streamlit application that can 2 plot live data but in the process i am getting the above mentioned error.

palceholder = st.empty()

with palceholder.container():
    while True:

        with open('data.txt','a') as f:
            f.write(str(random.randint(1+i,10+i)) + ',' + str(random.randint(1+i,10+i)) + '\n')

        with open("data.txt") as f:
            data = f.readlines()[-1]
            print(data)
            a,b = data.split(',')
            a = int(a)
            b = int(b)
            x.append(a)
            y.append(b)
            x2.append(a + 2)
            y2.append(b + 2)
            i += 1
            if x_max<a:
                x_max = a
            if y_max<b:
                y_max = b


        fig, ax = plt.subplots()
        ax.plot(x,y)
        st.pyplot(fig)

        fig2, ax2 = plt.subplots()
        ax2.plot(x2,y2)
        st.pyplot(fig2)

        time.sleep(0.1)

        palceholder.empty()

This error message pops up as soon as i open the application in the browser window.

I am unable to solve this issue. Please help me solve the issue…

Thank you for reading my request.

FWIW this is a minimal example that gives the same message:

import streamlit as st

palceholder = st.empty()
with palceholder.container():
    palceholder.empty()
    st.write("Stuff")

Looks like you can’t put anything in the container after emptying it, at least not within the same context manager.

Your code seems to work (with some glitches), after adding missing imports and initializing undefined variables, if you nest the context manager in the loop:

import random
import time

import streamlit as st
import matplotlib.pyplot as plt

i = 0
x = []
y = []
x2 = []
y2 = []
x_max = 0
y_max = 0

palceholder = st.empty()

while True:
    with palceholder.container():
        with open('data.txt','a') as f:
            f.write(str(random.randint(1+i,10+i)) + ',' + str(random.randint(1+i,10+i)) + '\n')

        with open("data.txt") as f:
            data = f.readlines()[-1]
            print(data)
            a,b = data.split(',')
            a = int(a)
            b = int(b)
            x.append(a)
            y.append(b)
            x2.append(a + 2)
            y2.append(b + 2)
            i += 1
            if x_max<a:
                x_max = a
            if y_max<b:
                y_max = b

        fig, ax = plt.subplots()
        ax.plot(x,y)
        st.pyplot(fig)

        fig2, ax2 = plt.subplots()
        ax2.plot(x2,y2)
        st.pyplot(fig2)

        time.sleep(0.1)

        palceholder.empty()
1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.