update name
This commit is contained in:
94
Home.py
Normal file
94
Home.py
Normal file
@@ -0,0 +1,94 @@
|
||||
import os
|
||||
|
||||
import streamlit as st
|
||||
from loguru import logger
|
||||
|
||||
st.set_page_config(
|
||||
page_title="Leo's Demo App",
|
||||
page_icon="🧊",
|
||||
layout="wide",
|
||||
initial_sidebar_state="expanded",
|
||||
)
|
||||
|
||||
|
||||
@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()
|
||||
st.session_state.file_lines = st.session_state.file_content.splitlines()
|
||||
|
||||
|
||||
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():
|
||||
new_file_name = st.session_state.new_file
|
||||
if not new_file_name.endswith('.py'):
|
||||
new_file_name = f'{new_file_name}.py'
|
||||
logger.debug(f"create new file: {new_file_name}")
|
||||
|
||||
content = '''
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding:utf-8 -*-
|
||||
import streamlit as st
|
||||
{add_content}
|
||||
st.write('Hello World!')
|
||||
'''
|
||||
with open(os.path.join(settings.BASE_DIR, 'pages', new_file_name), 'w') as f:
|
||||
content = content.format(add_content=f"""st.markdown("# {st.session_state.new_file}")""").strip()
|
||||
st.write(f"create new file: {new_file_name}, write: ")
|
||||
st.code(content, language='python')
|
||||
f.write(content)
|
||||
|
||||
|
||||
with st.sidebar:
|
||||
st.selectbox(
|
||||
'选择要配置的文件',
|
||||
pages,
|
||||
index=0,
|
||||
format_func=lambda x: x['filename'],
|
||||
on_change=open_selected_file,
|
||||
key='page_file'
|
||||
)
|
||||
st.write('You selected:', st.session_state.page_file)
|
||||
st.divider()
|
||||
st.text_input('新建文件', on_change=new_file, key='new_file')
|
||||
|
||||
if 'file_content' not in st.session_state:
|
||||
open_selected_file()
|
||||
|
||||
st.write(f'# 当前文件: `{st.session_state.page_file["filename"]}`')
|
||||
col1, col2 = st.columns(2)
|
||||
|
||||
with col1:
|
||||
st.write('### 代码高亮展示')
|
||||
st.code(st.session_state.file_content, line_numbers=True)
|
||||
|
||||
with col2:
|
||||
st.write('### 内容编辑区')
|
||||
st.text_area(
|
||||
key='file_content',
|
||||
label_visibility='collapsed',
|
||||
label='Text to analyze',
|
||||
on_change=save_file_content,
|
||||
value=st.session_state.file_content,
|
||||
height=50 + 22 * len(st.session_state.file_lines),
|
||||
)
|
||||
Reference in New Issue
Block a user