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) # Apply the format string to create init_problem init_problem = format_string.format(a=a, b=b) expression = expand(init_problem) # Convert the expression to a string expression_str = str(expression) # Replace x^2 with x² expression_str = expression_str.replace("x**2", "x²") expression_str = expression_str.replace("*", "") # Add the equality sign 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 input for init_problem format_string = st.text_input("init_problemのフォーマット文字列:", value="(x + {a})*(x + {b})") # Container to hold generated problems 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("問題をテキストファイルに保存し、ダウンロードしました。") # Content outside the container st.write("This is outside the container") if __name__ == "__main__": main()