charbelmalo commited on
Commit
bad0db0
·
1 Parent(s): 38d2cad

Add sorting by favorites and refactor Gradio Blocks for improved layout and interactivity

Browse files
Files changed (1) hide show
  1. app.py +54 -46
app.py CHANGED
@@ -54,6 +54,8 @@ def get_shortcuts_dataframe(sort_by='Recently Added', search_query='', filter_ta
54
  datafra = datafra.sort_values('name')
55
  elif sort_by == 'Recently Added':
56
  datafra = datafra.sort_values('date_added', ascending=False)
 
 
57
  # Reset index
58
  datafra = datafra.reset_index(drop=True)
59
  return datafra
@@ -113,59 +115,38 @@ def toggle_favorite(index):
113
  load_shortcuts()
114
 
115
  # Build the Gradio App
116
- with gr.Blocks(js="""
117
- <script>
118
- function togglePin(idx) {
119
- fetch('/toggle_pin', {
120
- method: 'POST',
121
- headers: {
122
- 'Content-Type': 'application/json'
123
- },
124
- body: JSON.stringify({'index': idx})
125
- }).then(response => response.json()).then(data => {
126
- const gridOutput = document.querySelector('#grid_output');
127
- gridOutput.innerHTML = data['grid_html'];
128
- });
129
- }
130
- function toggleFavorite(idx) {
131
- fetch('/toggle_favorite', {
132
- method: 'POST',
133
- headers: {
134
- 'Content-Type': 'application/json'
135
- },
136
- body: JSON.stringify({'index': idx})
137
- }).then(response => response.json()).then(data => {
138
- const gridOutput = document.querySelector('#grid_output');
139
- gridOutput.innerHTML = data['grid_html'];
140
- });
141
- }
142
- </script>
143
- """) as demo:
144
- gr.Markdown("## Add a New Website Shortcut")
145
- with gr.Row():
146
- name = gr.Textbox(label="Name")
147
- link = gr.Textbox(label="Link")
148
- with gr.Row():
149
- tags = gr.Textbox(label="Tags (separate levels with '/')")
150
- emojis = gr.Textbox(label="Emojis")
151
- with gr.Row():
152
- color_from = gr.ColorPicker(label="Gradient Color From")
153
- color_to = gr.ColorPicker(label="Gradient Color To")
154
- short_description = gr.Textbox(label="Short Description")
155
- add_button = gr.Button("Add Shortcut")
156
- # Output component for the updated grid
157
- grid_output = gr.HTML(elem_id="grid_output")
158
 
159
  # Actions for filters
160
- gr.Markdown("## Website Shortcuts")
161
  with gr.Row():
162
  search_bar = gr.Textbox(label="Search")
163
- sort_options = gr.Dropdown(choices=['Recently Added', 'Alphabetical'], label="Sort By", value='Recently Added')
164
  # Collect all unique tags
165
  def get_all_tags():
166
  return list(set(tag for shortcut in shortcuts_list for tag in shortcut['tags']))
167
  filter_tags = gr.CheckboxGroup(choices=get_all_tags(), label="Filter Tags")
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  # Update display when filters change
170
  def refresh_display(sort_by, search_query, filter_tags):
171
  return update_display(sort_by, search_query, filter_tags)
@@ -185,9 +166,36 @@ with gr.Blocks(js="""
185
  grid_output.value = update_display()
186
 
187
  # JavaScript functions for interactivity
188
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  # Inject custom JavaScript
190
- demo.load(fn=lambda: None, inputs=None, outputs=None)
191
 
192
  # Expose endpoints for toggle functions
193
  api = FastAPI()
 
54
  datafra = datafra.sort_values('name')
55
  elif sort_by == 'Recently Added':
56
  datafra = datafra.sort_values('date_added', ascending=False)
57
+ elif sort_by == 'Favorites':
58
+ datafra = datafra.sort_values('favorited')
59
  # Reset index
60
  datafra = datafra.reset_index(drop=True)
61
  return datafra
 
115
  load_shortcuts()
116
 
117
  # Build the Gradio App
118
+ with gr.Blocks(theme='charbelgrower/Crystal') as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  # Actions for filters
121
+ gr.Label("Website Shortcuts")
122
  with gr.Row():
123
  search_bar = gr.Textbox(label="Search")
124
+ sort_options = gr.Dropdown(choices=['Recently Added', 'Alphabetical', 'Favorites'], label="Sort By", value='Recently Added')
125
  # Collect all unique tags
126
  def get_all_tags():
127
  return list(set(tag for shortcut in shortcuts_list for tag in shortcut['tags']))
128
  filter_tags = gr.CheckboxGroup(choices=get_all_tags(), label="Filter Tags")
129
 
130
+
131
+ grid_output = gr.HTML(elem_id="grid_output")
132
+
133
+ gr.Markdown("## Add a New Website Shortcut")
134
+ with gr.Row():
135
+ name = gr.Textbox(label="Name")
136
+ link = gr.Textbox(label="Link")
137
+ with gr.Row():
138
+ with gr.Column():
139
+ tags = gr.Textbox(label="Tags (separate levels with '/')")
140
+ with gr.Column():
141
+ emojis = gr.Textbox(label="Emojis")
142
+ with gr.Column():
143
+ color_from = gr.ColorPicker(label="Gradient Color From")
144
+ with gr.Column():
145
+ color_to = gr.ColorPicker(label="Gradient Color To")
146
+ short_description = gr.Textbox(label="Short Description")
147
+ add_button = gr.Button("Add Shortcut")
148
+ # Output component for the updated grid
149
+
150
  # Update display when filters change
151
  def refresh_display(sort_by, search_query, filter_tags):
152
  return update_display(sort_by, search_query, filter_tags)
 
166
  grid_output.value = update_display()
167
 
168
  # JavaScript functions for interactivity
169
+ custom_js = """
170
+ <script>
171
+ function togglePin(idx) {
172
+ fetch('/toggle_pin', {
173
+ method: 'POST',
174
+ headers: {
175
+ 'Content-Type': 'application/json'
176
+ },
177
+ body: JSON.stringify({'index': idx})
178
+ }).then(response => response.json()).then(data => {
179
+ const gridOutput = document.querySelector('#grid_output');
180
+ gridOutput.innerHTML = data['grid_html'];
181
+ });
182
+ }
183
+ function toggleFavorite(idx) {
184
+ fetch('/toggle_favorite', {
185
+ method: 'POST',
186
+ headers: {
187
+ 'Content-Type': 'application/json'
188
+ },
189
+ body: JSON.stringify({'index': idx})
190
+ }).then(response => response.json()).then(data => {
191
+ const gridOutput = document.querySelector('#grid_output');
192
+ gridOutput.innerHTML = data['grid_html'];
193
+ });
194
+ }
195
+ </script>
196
+ """
197
  # Inject custom JavaScript
198
+ demo.load(fn=lambda: None, inputs=None, outputs=None, _js=custom_js)
199
 
200
  # Expose endpoints for toggle functions
201
  api = FastAPI()