gdarid commited on
Commit
f37dfe4
·
1 Parent(s): f08389e

display image simply

Browse files
Files changed (1) hide show
  1. streamlit_app.py +118 -124
streamlit_app.py CHANGED
@@ -1,124 +1,118 @@
1
- """
2
- Streamlit application
3
- """
4
- import streamlit as st
5
- from streamlit.errors import StreamlitAPIException
6
- from loguru import logger
7
-
8
- import lsystog as ls
9
-
10
-
11
- def on_change_selection():
12
- """
13
- Change the pattern when the starting pattern is changed
14
-
15
- :return: None
16
- """
17
- current_selection = st.session_state.my_selection
18
- st.session_state.my_pattern = current_selection
19
-
20
-
21
- @st.cache_data
22
- def load_img(pattern, colors, nb_iterations, apply_rotation):
23
- """
24
- Return an image computed from the parameters
25
-
26
- :return: image
27
- """
28
- func_transf = ls.strc_2_strc_90 if apply_rotation else None
29
- try:
30
- gls = ls.Lsystg(axiom=None, rules=None, nbiter=nb_iterations, patterns=[pattern], colors=colors,
31
- banned_colors='/', nb_dest=1, verbose=True, func_transf=func_transf)
32
- image = gls.img(img_fpath="", col_fond=(0, 0, 0, 255))
33
- except ls.LsystError as ex:
34
- st.warning(ex)
35
- st.stop()
36
- except Exception as ex:
37
- st.warning("Please verify your parameters. Special characters are not permitted in the pattern except for '?'")
38
- logger.error(f"Something went wrong : {ex}")
39
- st.stop()
40
- else:
41
- return image
42
-
43
-
44
- st.set_page_config(page_title="Gridz", page_icon="🖼️")
45
- st.markdown("# Gridz")
46
-
47
- VERBOSE = False # Set verbose to true for more printed information
48
- first_time = True # At start, no need to click the draw button
49
-
50
- MD1 = """
51
- You have the flexibility to define your own colors and pattern
52
-
53
- Simply click on "Draw" when you are satisfied with your new input :sunglasses:
54
- """
55
-
56
- MD2 = """
57
- The possible colors are :
58
- - R : Red
59
- - G : Green
60
- - B : Blue
61
- - W : White
62
- - K : Black
63
- - Y : Yellow
64
- - M : Magenta
65
- - O : Orange
66
- - D : Dim gray
67
- - F : Forest green
68
- - N : Navy
69
- - P : Purple
70
- - T : Background color (black)
71
- - ? : Random color
72
-
73
- The pattern assigns colors from left to right and from top to bottom, with each "row" separated by an underscore
74
-
75
- The pattern consists of "rotating" colors represented by digits and fixed colors (refer to the available colors mentioned above)
76
-
77
- To understand how the pattern functions, try drawing with just one iteration
78
- """
79
-
80
- EXAMPLES_LIST = ['00000_01210_02020_01210_00000', '012_120_201', '1001_0220_0220_1001',
81
- '00000_01110_01210_01110_00000', 'T000T_01210_02020_01210_T000T', '00000_01210_02T20_01210_00000',
82
- '1112T2_1T12T2_111222_1TTT2T_1TTT2T_1TTT2T']
83
-
84
- st.sidebar.markdown(MD1)
85
-
86
- input_selection = st.sidebar.selectbox('Choose a starting pattern', EXAMPLES_LIST,
87
- index=0, on_change=on_change_selection, key="my_selection")
88
-
89
- EXAMPLES = f"""
90
- Few possible patterns with 3 colors (GRB for example) that you can select
91
-
92
- - **:green[{EXAMPLES_LIST[1]}]** ( 3X3 )
93
- - **:green[{EXAMPLES_LIST[2]}]** ( 4X4 )
94
- - **:green[{EXAMPLES_LIST[3]}]** ( 5X5 )
95
- - **:green[{EXAMPLES_LIST[4]}]** ( 5X5 )
96
- - **:green[{EXAMPLES_LIST[5]}]** ( 5X5 )
97
- - **:green[1112T2_1T12T2_..._1TTT2T_1TTT2T]** ( 6X6 )
98
- """
99
-
100
- st.sidebar.markdown(EXAMPLES)
101
-
102
- st.sidebar.markdown(MD2)
103
-
104
- with st.form("my_form"):
105
- col = st.text_input('Colors', 'GRB', key='my_colors')
106
- pat = st.text_input('Pattern', EXAMPLES_LIST[0], key='my_pattern')
107
- rotation = st.checkbox("90° rotation", True)
108
-
109
- nb_iter = st.number_input('Number of iterations', value=4, min_value=1, max_value=10, format='%d')
110
-
111
- # Every form has a submit button
112
- submitted = st.form_submit_button("Draw")
113
- if submitted or first_time:
114
- first_time = False
115
- img = load_img(pat, col, nb_iter, rotation)
116
-
117
- try:
118
- st.write(st.image(img, caption='Generated image'))
119
- except StreamlitAPIException as exc:
120
- # Currently : streamlit.errors.StreamlitAPIException: `_repr_html_()` is not a valid Streamlit command.
121
- if VERBOSE:
122
- logger.warning(exc)
123
-
124
- st.markdown("---")
 
1
+ """
2
+ Streamlit application
3
+ """
4
+ import streamlit as st
5
+ from loguru import logger
6
+
7
+ import lsystog as ls
8
+
9
+
10
+ def on_change_selection():
11
+ """
12
+ Change the pattern when the starting pattern is changed
13
+
14
+ :return: None
15
+ """
16
+ current_selection = st.session_state.my_selection
17
+ st.session_state.my_pattern = current_selection
18
+
19
+
20
+ @st.cache_data
21
+ def load_img(pattern, colors, nb_iterations, apply_rotation):
22
+ """
23
+ Return an image computed from the parameters
24
+
25
+ :return: image
26
+ """
27
+ func_transf = ls.strc_2_strc_90 if apply_rotation else None
28
+ try:
29
+ gls = ls.Lsystg(axiom=None, rules=None, nbiter=nb_iterations, patterns=[pattern], colors=colors,
30
+ banned_colors='/', nb_dest=1, verbose=True, func_transf=func_transf)
31
+ image = gls.img(img_fpath="", col_fond=(0, 0, 0, 255))
32
+ except ls.LsystError as ex:
33
+ st.warning(ex)
34
+ st.stop()
35
+ except Exception as ex:
36
+ st.warning("Please verify your parameters. Special characters are not permitted in the pattern except for '?'")
37
+ logger.error(f"Something went wrong : {ex}")
38
+ st.stop()
39
+ else:
40
+ return image
41
+
42
+
43
+ st.set_page_config(page_title="Gridz", page_icon="🖼️")
44
+ st.markdown("# Gridz")
45
+
46
+ VERBOSE = False # Set verbose to true for more printed information
47
+ first_time = True # At start, no need to click the draw button
48
+
49
+ MD1 = """
50
+ You have the flexibility to define your own colors and pattern
51
+
52
+ Simply click on "Draw" when you are satisfied with your new input :sunglasses:
53
+ """
54
+
55
+ MD2 = """
56
+ The possible colors are :
57
+ - R : Red
58
+ - G : Green
59
+ - B : Blue
60
+ - W : White
61
+ - K : Black
62
+ - Y : Yellow
63
+ - M : Magenta
64
+ - O : Orange
65
+ - D : Dim gray
66
+ - F : Forest green
67
+ - N : Navy
68
+ - P : Purple
69
+ - T : Background color (black)
70
+ - ? : Random color
71
+
72
+ The pattern assigns colors from left to right and from top to bottom, with each "row" separated by an underscore
73
+
74
+ The pattern consists of "rotating" colors represented by digits and fixed colors (refer to the available colors mentioned above)
75
+
76
+ To understand how the pattern functions, try drawing with just one iteration
77
+ """
78
+
79
+ EXAMPLES_LIST = ['00000_01210_02020_01210_00000', '012_120_201', '1001_0220_0220_1001',
80
+ '00000_01110_01210_01110_00000', 'T000T_01210_02020_01210_T000T', '00000_01210_02T20_01210_00000',
81
+ '1112T2_1T12T2_111222_1TTT2T_1TTT2T_1TTT2T']
82
+
83
+ st.sidebar.markdown(MD1)
84
+
85
+ input_selection = st.sidebar.selectbox('Choose a starting pattern', EXAMPLES_LIST,
86
+ index=0, on_change=on_change_selection, key="my_selection")
87
+
88
+ EXAMPLES = f"""
89
+ Few possible patterns with 3 colors (GRB for example) that you can select
90
+
91
+ - **:green[{EXAMPLES_LIST[1]}]** ( 3X3 )
92
+ - **:green[{EXAMPLES_LIST[2]}]** ( 4X4 )
93
+ - **:green[{EXAMPLES_LIST[3]}]** ( 5X5 )
94
+ - **:green[{EXAMPLES_LIST[4]}]** ( 5X5 )
95
+ - **:green[{EXAMPLES_LIST[5]}]** ( 5X5 )
96
+ - **:green[1112T2_1T12T2_..._1TTT2T_1TTT2T]** ( 6X6 )
97
+ """
98
+
99
+ st.sidebar.markdown(EXAMPLES)
100
+
101
+ st.sidebar.markdown(MD2)
102
+
103
+ with st.form("my_form"):
104
+ col = st.text_input('Colors', 'GRB', key='my_colors')
105
+ pat = st.text_input('Pattern', EXAMPLES_LIST[0], key='my_pattern')
106
+ rotation = st.checkbox("90° rotation", True)
107
+
108
+ nb_iter = st.number_input('Number of iterations', value=4, min_value=1, max_value=10, format='%d')
109
+
110
+ # Every form has a submit button
111
+ submitted = st.form_submit_button("Draw")
112
+ if submitted or first_time:
113
+ first_time = False
114
+ img = load_img(pat, col, nb_iter, rotation)
115
+
116
+ st.image(img, caption='Generated image')
117
+
118
+ st.markdown("---")