NoCrypt commited on
Commit
ce02021
1 Parent(s): 8a6fac1

Create theme_dropdown.py

Browse files
Files changed (1) hide show
  1. theme_dropdown.py +57 -0
theme_dropdown.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pathlib
3
+
4
+ from gradio.themes.utils import ThemeAsset
5
+
6
+
7
+ def create_theme_dropdown():
8
+ import gradio as gr
9
+
10
+ asset_path = pathlib.Path(__file__).parent / "themes"
11
+ themes = []
12
+ for theme_asset in os.listdir(str(asset_path)):
13
+ themes.append(
14
+ (ThemeAsset(theme_asset), gr.Theme.load(str(asset_path / theme_asset)))
15
+ )
16
+
17
+ def make_else_if(theme_asset):
18
+ return f"""
19
+ else if (theme == '{str(theme_asset[0].version)}') {{
20
+ var theme_css = `{theme_asset[1]._get_theme_css()}`
21
+ }}"""
22
+
23
+ head, tail = themes[0], themes[1:]
24
+ if_statement = f"""
25
+ if (theme == "{str(head[0].version)}") {{
26
+ var theme_css = `{head[1]._get_theme_css()}`
27
+ }} {" ".join(make_else_if(t) for t in tail)}
28
+ """
29
+
30
+ latest_to_oldest = sorted([t[0] for t in themes], key=lambda asset: asset.version)[
31
+ ::-1
32
+ ]
33
+ latest_to_oldest = [str(t.version) for t in latest_to_oldest]
34
+
35
+ component = gr.Dropdown(
36
+ choices=latest_to_oldest,
37
+ value=latest_to_oldest[0],
38
+ render=False,
39
+ label="Select Version",
40
+ ).style(container=False)
41
+
42
+ return (
43
+ component,
44
+ f"""
45
+ (theme) => {{
46
+ if (!document.querySelector('.theme-css')) {{
47
+ var theme_elem = document.createElement('style');
48
+ theme_elem.classList.add('theme-css');
49
+ document.head.appendChild(theme_elem);
50
+ }} else {{
51
+ var theme_elem = document.querySelector('.theme-css');
52
+ }}
53
+ {if_statement}
54
+ theme_elem.innerHTML = theme_css;
55
+ }}
56
+ """,
57
+ )