| import time |
| import random |
| from sympy import symbols, expand |
| import streamlit as st |
|
|
| def generate_problem(min_value, max_value, format_string): |
| x = symbols('x') |
| a = random.randint(min_value, max_value) |
| b = random.randint(min_value, max_value) |
| |
| |
| init_problem = format_string.format(a=a, b=b) |
|
|
| expression = expand(init_problem) |
|
|
| |
| expression_str = str(expression) |
|
|
| |
| expression_str = expression_str.replace("x**2", "x²") |
| expression_str = expression_str.replace("*", "") |
| |
| equation_str = f"{expression_str} = 0" |
|
|
| return equation_str |
|
|
| def save_to_text(problems): |
| text_content = "" |
| for index, problem in enumerate(problems, start=1): |
| text_content += f"Problem {index}:\n{problem}\n\n" |
| return text_content |
|
|
| def main(): |
| st.title("数学問題生成アプリ") |
|
|
| |
| min_value = st.number_input("乱数の最小値:", value=1) |
| max_value = st.number_input("乱数の最大値:", value=10) |
| problem_count = st.number_input("問題数:", value=20) |
|
|
| |
| format_string = st.text_input("init_problemのフォーマット文字列:", value="(x + {a})*(x + {b})") |
|
|
| |
| with st.container(): |
| |
| if st.button("問題を生成してダウンロード"): |
| |
| generated_problems = [generate_problem(min_value, max_value, format_string) for _ in range(int(problem_count))] |
|
|
| |
| st.subheader("生成された問題:") |
| for index, problem in enumerate(generated_problems, start=1): |
| st.write(f"Problem {index}:\n{problem}\n") |
|
|
| |
| download_button = st.download_button( |
| label="テキストファイルをダウンロード", |
| data=save_to_text(generated_problems), |
| file_name="generated_problems.txt", |
| key="text_download" |
| ) |
|
|
| |
| if download_button: |
| st.success("問題をテキストファイルに保存し、ダウンロードしました。") |
|
|
| |
| st.write("This is outside the container") |
|
|
| if __name__ == "__main__": |
| main() |
|
|