File size: 2,545 Bytes
032d718
26f430d
 
 
 
4344296
26f430d
 
 
4344296
 
 
 
26f430d
 
 
 
 
 
 
18d965c
26f430d
 
 
 
 
18d965c
6c922f9
 
 
 
26f430d
 
 
 
 
 
 
 
 
4344296
 
 
6c922f9
 
 
 
 
4344296
fe0af3c
6c922f9
fcb5db8
 
 
fe0af3c
6c922f9
 
 
 
 
 
 
 
 
 
 
fcb5db8
6c922f9
 
26f430d
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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()