44 lines
1.0 KiB
Python
44 lines
1.0 KiB
Python
import os
|
|
|
|
import streamlit as st
|
|
|
|
from app.core import settings
|
|
|
|
|
|
def select_file():
|
|
...
|
|
|
|
|
|
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'
|
|
with open(os.path.join(settings.BASE_DIR, 'pages', new_file_name), 'w') as f:
|
|
f.write('''
|
|
#!/usr/bin/env python3
|
|
# -*- coding:utf-8 -*-
|
|
import streamlit as st
|
|
st.write('Hello World!')
|
|
''')
|
|
|
|
|
|
with st.sidebar:
|
|
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']
|
|
|
|
option = st.selectbox(
|
|
'How would you like to be contacted?',
|
|
pages,
|
|
index=0,
|
|
format_func=lambda x: x['filename'],
|
|
on_change=select_file,
|
|
key='page_file'
|
|
)
|
|
|
|
st.write('You selected:', option)
|
|
|
|
st.text_input('新建文件', on_change=new_file, key='new_file')
|