为什么会出现错误说st.session_state在streamlit中没有键“new_todo”

问题描述 投票:0回答:0

我正在学习一门课程,该课程创建了这个程序(该课程是 Python Mega 课程:60 天学习 Python,构建 20 个应用程序)。当我使用streamlit run web.py 运行程序时,它说我没有初始化密钥。我以为我在第 17 行做了

web.py

import streamlit as st
import functions
todos = functions.get_todos()

def add_todo():
    todo = st.session_state["todo"]
    print(todo)

st.title("My Todo App")
st.subheader("This is my Todo App")
st.write("This app is to increase your productivity.")

for todo in todos:
    st.checkbox(todo)

st.text_input(label="", placeholder="Add New Todo...",
          on_change=add_todo(), key ='todo')

函数.py

FILEPATH =“todos.txt”

def get_todos(filepath=FILEPATH):
    """ Return Contents of File in List"""
    with open(filepath, 'r') as file_local:
        todos_local = file_local.readlines()
    return todos_local


def write_todos(todos_arg, filepath=FILEPATH):
    """Write a To-Do in Text File."""
    with open(filepath, 'w') as file:
        file.writelines(todos_arg)


if __name__ == "__main__":
    print("Hello")

我已重新启动该应用程序。我还尝试重命名密钥并将函数移到代码中的较低位置,以防它无法检测到密钥。

streamlit keyerror
© www.soinside.com 2019 - 2024. All rights reserved.