display image simply
Browse files- 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
|
| 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 |
-
st.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
st.
|
| 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 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
'
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
- **:green[{EXAMPLES_LIST[
|
| 93 |
-
- **:green[{EXAMPLES_LIST[
|
| 94 |
-
- **:green[{EXAMPLES_LIST[
|
| 95 |
-
- **:green[{EXAMPLES_LIST[
|
| 96 |
-
- **:green[
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
submitted
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 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("---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|