ysharma HF staff commited on
Commit
827e64d
1 Parent(s): 3ae8547

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -0
app.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+
4
+ def get_theme_css(theme):
5
+ themes = {
6
+ "light": {
7
+ "background": "#ffffff",
8
+ "text": "#000000",
9
+ "accent": "#2196f3"
10
+ },
11
+ "dark": {
12
+ "background": "#1a1a1a",
13
+ "text": "#ffffff",
14
+ "accent": "#64b5f6"
15
+ },
16
+ "sepia": {
17
+ "background": "#f4ecd8",
18
+ "text": "#5c4b37",
19
+ "accent": "#8b4513"
20
+ }
21
+ }
22
+ return themes.get(theme, themes["light"])
23
+
24
+ def get_font_size(font_size):
25
+ font_sizes = {
26
+ "small": "14px",
27
+ "medium": "16px",
28
+ "large": "18px"
29
+ }
30
+ return font_sizes.get(font_size, "16px")
31
+
32
+ with gr.Blocks() as demo:
33
+ # Store theme preference and custom settings in browser state
34
+ browser_state = gr.BrowserState({ #"user_preferences":
35
+ "theme": "light",
36
+ "font_size": "medium",})
37
+
38
+ with gr.Row():
39
+ gr.Markdown("# Theme Customization Demo")
40
+
41
+ with gr.Row():
42
+ theme_dropdown = gr.Dropdown(
43
+ choices=["light", "dark", "sepia"],
44
+ value="light",
45
+ label="Select Theme"
46
+ )
47
+ font_size = gr.Radio(
48
+ choices=["small", "medium", "large"],
49
+ value="medium",
50
+ label="Font Size"
51
+ )
52
+
53
+ preview = gr.HTML()
54
+
55
+
56
+ def update_preview(theme, font_size,):
57
+ theme_css = get_theme_css(theme)
58
+ font_size = get_font_size(font_size)
59
+
60
+ # Create a preview of the selected settings
61
+ preview_html = f"""
62
+ <div style="
63
+ background-color: {theme_css['background']};
64
+ color: {theme_css['text']};
65
+ padding: 20px;
66
+ border-radius: 8px;
67
+ font-size: {font_size};
68
+ display: grid;
69
+ gap: 20px;
70
+ ">
71
+ <div>
72
+ <h2 style="color: {theme_css['accent']}">Preview Content</h2>
73
+ <p>This is how your content will look with the selected theme and settings.</p>
74
+ <button style="
75
+ background-color: {theme_css['accent']};
76
+ color: {theme_css['background']};
77
+ border: none;
78
+ padding: 8px 16px;
79
+ border-radius: 4px;
80
+ cursor: pointer;
81
+ ">Sample Button</button>
82
+ </div>
83
+ </div>
84
+ """
85
+ return preview_html
86
+
87
+ def save_preferences(theme, font_size, ):
88
+ return {
89
+ "theme": theme,
90
+ "font_size": font_size,
91
+ }
92
+
93
+ def load_preferences(saved_prefs):
94
+ if saved_prefs is None:
95
+ saved_prefs = {
96
+ "theme": "light",
97
+ "font_size": "medium",
98
+ }
99
+ return (
100
+ saved_prefs["theme"],
101
+ saved_prefs["font_size"],
102
+ update_preview(
103
+ saved_prefs["theme"],
104
+ saved_prefs["font_size"],
105
+ )
106
+ )
107
+
108
+ # Update preview when any setting changes
109
+ #for input_component in [theme_dropdown, font_size, ]:
110
+ theme_dropdown.change(
111
+ fn=update_preview,
112
+ inputs=[theme_dropdown, font_size, ],
113
+ outputs=[preview]
114
+ )
115
+ font_size.change(
116
+ fn=update_preview,
117
+ inputs=[theme_dropdown, font_size, ],
118
+ outputs=[preview]
119
+ )
120
+ # Save to browser state when any setting changes
121
+ theme_dropdown.change(
122
+ fn=save_preferences,
123
+ inputs=[theme_dropdown, font_size, ],
124
+ outputs=[browser_state]
125
+ )
126
+ font_size.change(
127
+ fn=save_preferences,
128
+ inputs=[theme_dropdown, font_size, ],
129
+ outputs=[browser_state]
130
+ )
131
+
132
+ # Load saved preferences when the page loads
133
+ demo.load(
134
+ fn=load_preferences,
135
+ inputs=[browser_state],
136
+ outputs=[theme_dropdown, font_size, preview]
137
+ )
138
+
139
+ demo.launch()