增加效果 缓存settings

This commit is contained in:
leo
2023-12-31 18:35:26 +08:00
parent 44ef312811
commit 2f90a83737
3 changed files with 26 additions and 17 deletions

25
main.py
View File

@@ -3,15 +3,6 @@ import os
import streamlit as st
from loguru import logger
from app.core import settings
pages = os.listdir(os.path.join(settings.BASE_DIR, 'pages'))
pages = [{
'filename': page,
'filepath': os.path.join(settings.BASE_DIR, 'pages', page)
} for page in pages if page.endswith('.py') and page != '__init__.py']
pages.sort(key=lambda x: x['filename'])
st.set_page_config(
page_title="Leo's Cool App",
page_icon="🧊",
@@ -20,6 +11,21 @@ st.set_page_config(
)
@st.cache_resource
def get_settings():
from app.core import settings
return settings
settings = get_settings()
pages = os.listdir(os.path.join(settings.BASE_DIR, 'pages'))
pages = [{
'filename': page,
'filepath': os.path.join(settings.BASE_DIR, 'pages', page)
} for page in pages if page.endswith('.py') and page != '__init__.py']
pages.sort(key=lambda x: x['filename'])
def open_selected_file():
with open(st.session_state.page_file['filepath'], 'r', encoding='utf-8') as f:
st.session_state.file_content = f.read().strip()
@@ -30,6 +36,7 @@ def save_file_content():
with open(st.session_state.page_file['filepath'], 'w', encoding='utf-8') as f:
logger.debug(f"save: <\n{st.session_state.file_content}\n>to file {st.session_state.page_file['filepath']}")
f.write(st.session_state.file_content)
st.toast('保存成功!', icon='🎉')
def new_file():

10
pages/first_demo.py Normal file
View File

@@ -0,0 +1,10 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import streamlit as st
st.write('# Hello World!')
st.link_button("Go to gallery", "https://streamlit.io/gallery")
for i in range(5):
st.write(f"{i + 1}. this is line {i + 1}")

View File

@@ -1,8 +0,0 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import streamlit as st
st.write('Hello World!')
for i in range(1, 13):
st.write(f"{i}. this is line {i}")