Код: Выделить всё
import streamlit as st
import tkinter as tk
from tkinter import filedialog
import os
import pandas as pd
from pathlib import Path
uploaded_files = st.file_uploader("Upload your Python/SQL/C file", type=["py","sql","c"], accept_multiple_files=True)
st.session_state.file_contents = ""
root = tk.Tk()
root.withdraw()
root.wm_attributes('-topmost', 1)
st.write('Please select a folder:')
clicked = st.button('Browse Folder')
if clicked:
dirname = str(filedialog.askdirectory(master=root))
files = [file for file in os.listdir(dirname)]
output = pd.DataFrame({"File Name": files})
st.table(output)
for file in files:
st.session_state.file_contents += Path(os.path.join(dirname, file)).read_text()
for uploaded_file in uploaded_files:
st.session_state.file_contents += uploaded_file.read().decode('utf-8') + "\n"
print("File content initially:",st.session_state.file_contents)
До сих пор код работает нормально. Но когда я пишу код как: < /p>
Код: Выделить всё
if (uploaded_files and st.session_state.file_contents and st.session_state.messages) is not None:
for message in st.session_state.messages:
if message["role"] == "user":
st.chat_message("human").markdown(message["content"])
if message["role"] == "ai":
st.chat_message("ai").markdown(message["content"])
if prompt := st.chat_input("Generate test cases for the attached file(s)"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
print("File content before calling a function:",st.session_state.file_contents)
Итак, может ли кто -нибудь объяснить, почему я не получаю содержимое файла 2 , почему он исчезает после вызова St.chat_input () ?
Любая помощь будет оценен.
Подробнее здесь: https://stackoverflow.com/questions/794 ... -streamlit