This commit is contained in:
@@ -8,7 +8,7 @@ from enums import CounterType
|
||||
|
||||
@dialog("Add New Counter", icon=":material/add_box:")
|
||||
def _add_counter():
|
||||
colors = crud.get_colors(1)
|
||||
colors = crud.get_colors()
|
||||
with st.form(key="add_counter", border=False, clear_on_submit=True):
|
||||
title = st.text_input("Title:", key="new_counter_title")
|
||||
counter_type_name = st.selectbox("Type", options=[e.name for e in CounterType], key="new_counter_type")
|
||||
|
||||
58
app/pages/settings.py
Normal file
58
app/pages/settings.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import streamlit as st
|
||||
from pandas.core.ops.docstrings import key
|
||||
from pandas.io.formats.style import Styler
|
||||
|
||||
import queries.crud as crud
|
||||
|
||||
is_login_enabled = hasattr(st, 'user') and hasattr(st.user, 'is_logged_in')
|
||||
is_logged_in = is_login_enabled and st.user.is_logged_in
|
||||
|
||||
st.title("Settings")
|
||||
|
||||
if hasattr(st.session_state, 'user_name'):
|
||||
st.markdown(f"Currently logged in as **{st.session_state.user_name}**")
|
||||
|
||||
st.header("Colors")
|
||||
with st.container(key="settings-color-selector"):
|
||||
palettes = crud.get_color_palettes()
|
||||
selected = crud.get_color_palette()
|
||||
for palette in palettes.iterrows():
|
||||
id = palette[1]['id']
|
||||
name = palette[1]['name']
|
||||
color1 = palette[1]['color1']
|
||||
color2 = palette[1]['color2']
|
||||
color3 = palette[1]['color3']
|
||||
color4 = palette[1]['color4']
|
||||
|
||||
if selected == id:
|
||||
with st.container(horizontal=True, key=f"settings-color-selector-selected"):
|
||||
st.button(f"{name} **(selected)**", disabled=True, width="stretch", icon=":material/radio_button_checked:")
|
||||
st.html(f"""
|
||||
<span class="settings-color-selector-colors">
|
||||
<span style="background-color:#{color1}"> </span>
|
||||
<span style="background-color:#{color2}"> </span>
|
||||
<span style="background-color:#{color3}"> </span>
|
||||
<span style="background-color:#{color4}"> </span>
|
||||
</span>
|
||||
""", width=400)
|
||||
else:
|
||||
with st.container(horizontal=True):
|
||||
if st.button(f"{name}", width="stretch", icon=":material/radio_button_unchecked:"):
|
||||
crud.set_color_palette(id)
|
||||
st.rerun()
|
||||
|
||||
st.html(f"""
|
||||
<span class="settings-color-selector-colors">
|
||||
<span style="background-color:#{color1}"> </span>
|
||||
<span style="background-color:#{color2}"> </span>
|
||||
<span style="background-color:#{color3}"> </span>
|
||||
<span style="background-color:#{color4}"> </span>
|
||||
</span>
|
||||
""", width=400)
|
||||
|
||||
|
||||
st.header("Actions")
|
||||
with st.container():
|
||||
if is_logged_in:
|
||||
if st.button("Logout", icon=":material/logout:", width="stretch"):
|
||||
st.logout()
|
||||
@@ -79,9 +79,47 @@ def get_counter(counter_id:int):
|
||||
return None
|
||||
|
||||
|
||||
def get_colors(palette_id:int):
|
||||
def get_color_palettes():
|
||||
try:
|
||||
return connection().query('''SELECT color1,color2,color3,color4,color5 FROM color_palettes WHERE id = :id''', params={'id': palette_id})
|
||||
return connection().query('SELECT * FROM color_palettes''')
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return None
|
||||
|
||||
|
||||
def get_color_palette():
|
||||
user_id = int(st.session_state.user_id)
|
||||
try:
|
||||
return int(connection().query('SELECT color_palette_id FROM users WHERE id = :id''', params={'id': user_id})['color_palette_id'][0])
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return None
|
||||
|
||||
|
||||
def set_color_palette(palette_id:int):
|
||||
user_id = int(st.session_state.user_id)
|
||||
logger.info("Changing palette for user %d to %d", user_id, palette_id)
|
||||
with connection().session as session:
|
||||
try:
|
||||
query = text('UPDATE users SET color_palette_id = :palette WHERE id = :user')
|
||||
session.execute(query, {
|
||||
'palette': palette_id,
|
||||
'user': user_id
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
session.rollback()
|
||||
|
||||
|
||||
def get_colors():
|
||||
user_id = int(st.session_state.user_id)
|
||||
try:
|
||||
return connection().query('''
|
||||
SELECT color1,color2,color3,color4,color5
|
||||
FROM users u
|
||||
LEFT JOIN color_palettes p ON p.id = u.color_palette_id
|
||||
WHERE u.id = :id
|
||||
''', params={'id': user_id})
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return None
|
||||
|
||||
@@ -4,18 +4,12 @@ import queries.user as user_queries
|
||||
|
||||
from logger import init_logger
|
||||
from styles import init_styles
|
||||
from user import init_user, is_login_enabled, is_logged_in
|
||||
|
||||
init_logger()
|
||||
init_user()
|
||||
init_styles()
|
||||
|
||||
is_login_enabled = hasattr(st, 'user')
|
||||
is_logged_in = is_login_enabled and hasattr(st.user, 'is_logged_in') and st.user.is_logged_in
|
||||
|
||||
if is_logged_in:
|
||||
user_queries.set_user_in_session(st.user)
|
||||
else:
|
||||
st.session_state.user_id = 1 # default user
|
||||
|
||||
if is_login_enabled and not is_logged_in:
|
||||
with st.container(width="stretch", height="stretch", horizontal_alignment="center"):
|
||||
st.title("Daily Counter", width="stretch", text_alignment="center")
|
||||
@@ -27,11 +21,7 @@ if is_login_enabled and not is_logged_in:
|
||||
else:
|
||||
counters = st.Page("pages/counters.py", title="Counters", icon=":material/update:")
|
||||
stats = st.Page("pages/stats.py", title="Statistics", icon=":material/chart_data:")
|
||||
logoutPage = st.Page(st.logout, title="Logout", icon=":material/logout:")
|
||||
|
||||
pages = [counters, stats]
|
||||
if is_login_enabled:
|
||||
pages = pages + [logoutPage]
|
||||
|
||||
settings = st.Page("pages/settings.py", title=" ", icon=":material/menu:")
|
||||
pages = [counters, stats, settings]
|
||||
pg = st.navigation(position="top", pages=pages)
|
||||
pg.run()
|
||||
|
||||
@@ -8,7 +8,7 @@ def _load_css(filepath):
|
||||
|
||||
|
||||
def _load_color_selector_styles():
|
||||
colors = crud.get_colors(1) #FIXME Change to use user profile color palette
|
||||
colors = crud.get_colors()
|
||||
for idx, c in enumerate(colors.keys()):
|
||||
css_color = '#' + colors[c][0]
|
||||
st.html(f"""
|
||||
|
||||
11
app/user.py
Normal file
11
app/user.py
Normal file
@@ -0,0 +1,11 @@
|
||||
import queries.user as user_queries
|
||||
import streamlit as st
|
||||
|
||||
is_login_enabled = hasattr(st, 'user') and hasattr(st.user, 'is_logged_in')
|
||||
is_logged_in = is_login_enabled and st.user.is_logged_in
|
||||
|
||||
def init_user():
|
||||
if is_logged_in:
|
||||
user_queries.set_user_in_session(st.user)
|
||||
else:
|
||||
st.session_state.user_id = 1 # default user
|
||||
Reference in New Issue
Block a user