whitphx HF staff commited on
Commit
3e87437
1 Parent(s): d76edd7

Refactoring

Browse files
Files changed (2) hide show
  1. app.py +4 -26
  2. util.py +26 -0
app.py CHANGED
@@ -1,17 +1,10 @@
1
  import colorsys
2
- from typing import NamedTuple
3
 
4
  import streamlit as st
5
 
6
  import fragments
7
-
8
  import util
9
-
10
- class ThemeColor(NamedTuple):
11
- primaryColor: str
12
- backgroundColor: str
13
- secondaryBackgroundColor: str
14
- textColor: str
15
 
16
 
17
  preset_colors: list[tuple[str, ThemeColor]] = [
@@ -29,24 +22,9 @@ preset_colors: list[tuple[str, ThemeColor]] = [
29
  ))
30
  ]
31
 
32
- if 'theme_from_initial_config' not in st.session_state:
33
- config_theme_primaryColor = st._config.get_option(f'theme.primaryColor')
34
- config_theme_backgroundColor = st._config.get_option(f'theme.backgroundColor')
35
- config_theme_secondaryBackgroundColor = st._config.get_option(f'theme.secondaryBackgroundColor')
36
- config_theme_textColor = st._config.get_option(f'theme.textColor')
37
- if config_theme_primaryColor and config_theme_backgroundColor and config_theme_secondaryBackgroundColor and config_theme_textColor:
38
- st.session_state['theme_from_initial_config'] = ThemeColor(
39
- primaryColor=config_theme_primaryColor,
40
- backgroundColor=config_theme_backgroundColor,
41
- secondaryBackgroundColor=config_theme_secondaryBackgroundColor,
42
- textColor=config_theme_textColor,
43
- )
44
-
45
- if 'theme_from_initial_config' in st.session_state:
46
- preset_colors.append((
47
- "From the config",
48
- st.session_state['theme_from_initial_config'],
49
- ))
50
 
51
  default_color = preset_colors[0][1]
52
 
 
1
  import colorsys
 
2
 
3
  import streamlit as st
4
 
5
  import fragments
 
6
  import util
7
+ from util import ThemeColor
 
 
 
 
 
8
 
9
 
10
  preset_colors: list[tuple[str, ThemeColor]] = [
 
22
  ))
23
  ]
24
 
25
+ theme_from_initial_config = util.get_config_theme_color()
26
+ if theme_from_initial_config:
27
+ preset_colors.append(("From the config", theme_from_initial_config))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  default_color = preset_colors[0][1]
30
 
util.py CHANGED
@@ -1,10 +1,36 @@
1
  import re
2
  import random
3
  from colorsys import hls_to_rgb
 
4
 
 
5
  import wcag_contrast_ratio as contrast
6
 
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def parse_hex(rgb_hex_str: str) -> tuple[float, float, float]:
9
  if not re.match(r"^#[0-9a-fA-F]{6}$", rgb_hex_str):
10
  raise ValueError("Invalid hex color")
 
1
  import re
2
  import random
3
  from colorsys import hls_to_rgb
4
+ from typing import NamedTuple
5
 
6
+ import streamlit as st
7
  import wcag_contrast_ratio as contrast
8
 
9
 
10
+ class ThemeColor(NamedTuple):
11
+ primaryColor: str
12
+ backgroundColor: str
13
+ secondaryBackgroundColor: str
14
+ textColor: str
15
+
16
+
17
+ @st.cache_resource
18
+ def get_config_theme_color():
19
+ config_theme_primaryColor = st._config.get_option(f'theme.primaryColor')
20
+ config_theme_backgroundColor = st._config.get_option(f'theme.backgroundColor')
21
+ config_theme_secondaryBackgroundColor = st._config.get_option(f'theme.secondaryBackgroundColor')
22
+ config_theme_textColor = st._config.get_option(f'theme.textColor')
23
+ if config_theme_primaryColor and config_theme_backgroundColor and config_theme_secondaryBackgroundColor and config_theme_textColor:
24
+ return ThemeColor(
25
+ primaryColor=config_theme_primaryColor,
26
+ backgroundColor=config_theme_backgroundColor,
27
+ secondaryBackgroundColor=config_theme_secondaryBackgroundColor,
28
+ textColor=config_theme_textColor,
29
+ )
30
+
31
+ return None
32
+
33
+
34
  def parse_hex(rgb_hex_str: str) -> tuple[float, float, float]:
35
  if not re.match(r"^#[0-9a-fA-F]{6}$", rgb_hex_str):
36
  raise ValueError("Invalid hex color")