diff --git a/Dockerfile b/Dockerfile index 6f0763b..1724eb4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,4 +8,4 @@ COPY . /app WORKDIR /app -CMD ["streamlit", "run", "/app/main.py", "--server.port", "80", "--server.enableCORS", "false"] \ No newline at end of file +CMD ["/app/start.sh"] \ No newline at end of file diff --git a/main.py b/main.py index 1684a94..cfa6933 100644 --- a/main.py +++ b/main.py @@ -5,9 +5,30 @@ 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']) -def select_file(): - ... +st.set_page_config( + page_title="Leo's Cool App", + page_icon="🧊", + layout="wide", + initial_sidebar_state="expanded", +) + + +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: + f.write(st.session_state.file_content) def new_file(): @@ -31,21 +52,35 @@ 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?', + st.selectbox( + '选择要配置的文件', pages, index=0, format_func=lambda x: x['filename'], - on_change=select_file, + 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') - st.write('You selected:', option) +if 'file_content' not in st.session_state: + open_selected_file() -st.text_input('新建文件', on_change=new_file, key='new_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), + ) diff --git a/pages/form.py b/pages/form.py deleted file mode 100644 index 81a340f..0000000 --- a/pages/form.py +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding:utf-8 -*- -import streamlit as st -import pandas as pd -import numpy as np - - -def get_data(): - df = pd.DataFrame({ - "lat": np.random.randn(200) / 50 + 37.76, - "lon": np.random.randn(200) / 50 + -122.4, - "team": ['A', 'B'] * 100 - }) - return df - - -if st.button('Generate new points'): - st.session_state.df = get_data() -if 'df' not in st.session_state: - st.session_state.df = get_data() -df = st.session_state.df - -with st.form("my_form"): - header = st.columns([1, 2, 2]) - header[0].subheader('Color') - header[1].subheader('Opacity') - header[2].subheader('Size') - - row1 = st.columns([1, 2, 2]) - colorA = row1[0].color_picker('Team A', '#0000FF') - opacityA = row1[1].slider('A opacity', 20, 100, 50, label_visibility='hidden') - sizeA = row1[2].slider('A size', 50, 200, 100, step=10, label_visibility='hidden') - - row2 = st.columns([1, 2, 2]) - colorB = row2[0].color_picker('Team B', '#FF0000') - opacityB = row2[1].slider('B opacity', 20, 100, 50, label_visibility='hidden') - sizeB = row2[2].slider('B size', 50, 200, 100, step=10, label_visibility='hidden') - - st.form_submit_button('Update map') - -alphaA = int(opacityA * 255 / 100) -alphaB = int(opacityB * 255 / 100) - -df['color'] = np.where(df.team == 'A', colorA + f'{alphaA:02x}', colorB + f'{alphaB:02x}') -df['size'] = np.where(df.team == 'A', sizeA, sizeB) - -st.map(df, size='size', color='color') diff --git a/pages/test.py b/pages/test.py new file mode 100644 index 0000000..395d8a1 --- /dev/null +++ b/pages/test.py @@ -0,0 +1,8 @@ +#!/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}") diff --git a/pages/test2_input-output.py b/pages/test2_input-output.py new file mode 100644 index 0000000..f5b4c62 --- /dev/null +++ b/pages/test2_input-output.py @@ -0,0 +1,31 @@ +import random + +import streamlit as st +import pandas as pd +import numpy as np + +if "counter" not in st.session_state: + st.session_state.counter = 0 + +st.session_state.counter += 1 + +st.header(f"This page has run {st.session_state.counter} times.") +st.button("Run it again") + +if "df" not in st.session_state: + st.session_state.df = pd.DataFrame(np.random.randn(20, 2), columns=["x", "y"]) +if "color" not in st.session_state: + st.session_state.color = "#FF0000" + + +def generate_random_color(): + rand_color = "#{:06x}".format(random.randint(0, 0xFFFFFF)) + st.session_state.color = rand_color.upper() + + +st.header("Choose a datapoint color") +st.color_picker("Color", "#FF0000", key="color") +st.button("random color", key="random_color", on_click=generate_random_color) +st.divider() + +st.scatter_chart(st.session_state.df, x="x", y="y", color=st.session_state.color) diff --git a/requirements.txt b/requirements.txt index 4883385..274b1e8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,6 @@ streamlit~=1.29.0 loguru pydantic redis -pydantic-settings \ No newline at end of file +pydantic-settings +pandas +numpy \ No newline at end of file diff --git a/start.bat b/start.bat index b828833..bc87773 100644 --- a/start.bat +++ b/start.bat @@ -1 +1 @@ -streamlit run main.py \ No newline at end of file +streamlit run --server.port 5280 --server.enableCORS false ./main.py \ No newline at end of file diff --git a/start.sh b/start.sh index b828833..b7638ed 100644 --- a/start.sh +++ b/start.sh @@ -1 +1,2 @@ -streamlit run main.py \ No newline at end of file +#!/usr/bin/env bash +streamlit run --server.port 80 --server.enableCORS false ./main.py \ No newline at end of file