Spaces:
Running
Running
charbelmalo
commited on
Commit
Β·
0509023
1
Parent(s):
332ea16
Add image generation for shortcuts and update display logic in app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
|
|
2 |
from datetime import datetime
|
3 |
import json
|
4 |
import pandas as pd
|
|
|
|
|
5 |
|
6 |
shortcuts_list = []
|
7 |
|
@@ -34,7 +36,6 @@ def add_shortcut(name, tags, link, emojis, color_from, color_to, short_descripti
|
|
34 |
}
|
35 |
shortcuts_list.append(new_shortcut)
|
36 |
save_shortcuts()
|
37 |
-
# Return updated DataFrame
|
38 |
return update_display()
|
39 |
|
40 |
def get_shortcuts_dataframe(sort_by='Recently Added', search_query='', filter_tags=[]):
|
@@ -54,55 +55,68 @@ def get_shortcuts_dataframe(sort_by='Recently Added', search_query='', filter_ta
|
|
54 |
datafra = datafra.sort_values('date_added', ascending=False)
|
55 |
# Reset index
|
56 |
datafra = datafra.reset_index(drop=True)
|
57 |
-
# Select relevant columns
|
58 |
-
datafra = datafra[['name', 'emojis', 'short_description', 'link', 'pinned', 'favorited']]
|
59 |
return datafra
|
60 |
|
61 |
-
def
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
datafra = datafra.rename(columns={
|
67 |
-
'name': 'Name',
|
68 |
-
'emojis': 'Emojis',
|
69 |
-
'short_description': 'Description',
|
70 |
-
'link': 'Link',
|
71 |
-
'pinned': 'Pinned',
|
72 |
-
'favorited': 'Favorited'
|
73 |
-
})
|
74 |
-
return datafra
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
return update_display()
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
load_shortcuts()
|
104 |
|
105 |
-
# Build the Gradio App
|
106 |
with gr.Blocks() as app:
|
107 |
gr.Markdown("## Add a New Website Shortcut")
|
108 |
with gr.Row():
|
@@ -116,50 +130,107 @@ with gr.Blocks() as app:
|
|
116 |
color_to = gr.ColorPicker(label="Gradient Color To")
|
117 |
short_description = gr.Textbox(label="Short Description")
|
118 |
add_button = gr.Button("Add Shortcut")
|
119 |
-
# Output component for the updated table
|
120 |
-
shortcuts_table = gr.Dataframe(headers=['Name', 'Emojis', 'Description', 'Link', 'Pinned', 'Favorited'], datatype=['str', 'str', 'str', 'str', 'bool', 'bool'], interactive=False)
|
121 |
-
|
122 |
add_button.click(
|
123 |
add_shortcut,
|
124 |
inputs=[name, tags, link, emojis, color_from, color_to, short_description],
|
125 |
-
outputs=
|
126 |
)
|
127 |
|
128 |
gr.Markdown("## Website Shortcuts")
|
129 |
with gr.Row():
|
130 |
search_bar = gr.Textbox(label="Search")
|
131 |
sort_options = gr.Dropdown(choices=['Recently Added', 'Alphabetical'], label="Sort By", value='Recently Added')
|
132 |
-
|
133 |
-
|
134 |
-
filter_tags = gr.CheckboxGroup(choices=all_tags, label="Filter Tags")
|
135 |
|
136 |
-
#
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
pin_button = gr.Button("π Pin/Unpin")
|
140 |
fav_button = gr.Button("β€οΈ Favorite/Unfavorite")
|
141 |
open_link_button = gr.Button("π Open Link")
|
142 |
-
|
143 |
-
#
|
144 |
-
def
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
|
165 |
app.launch()
|
|
|
2 |
from datetime import datetime
|
3 |
import json
|
4 |
import pandas as pd
|
5 |
+
from PIL import Image, ImageDraw, ImageFont
|
6 |
+
import io
|
7 |
|
8 |
shortcuts_list = []
|
9 |
|
|
|
36 |
}
|
37 |
shortcuts_list.append(new_shortcut)
|
38 |
save_shortcuts()
|
|
|
39 |
return update_display()
|
40 |
|
41 |
def get_shortcuts_dataframe(sort_by='Recently Added', search_query='', filter_tags=[]):
|
|
|
55 |
datafra = datafra.sort_values('date_added', ascending=False)
|
56 |
# Reset index
|
57 |
datafra = datafra.reset_index(drop=True)
|
|
|
|
|
58 |
return datafra
|
59 |
|
60 |
+
def generate_shortcut_image(shortcut):
|
61 |
+
# Create an image with gradient background
|
62 |
+
width, height = 300, 200
|
63 |
+
image = Image.new('RGB', (width, height))
|
64 |
+
draw = ImageDraw.Draw(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
# Draw gradient
|
67 |
+
color_from = shortcut['color_from']
|
68 |
+
color_to = shortcut['color_to']
|
69 |
+
r1, g1, b1 = int(color_from[1:3], 16), int(color_from[3:5], 16), int(color_from[5:], 16)
|
70 |
+
r2, g2, b2 = int(color_to[1:3], 16), int(color_to[3:5], 16), int(color_to[5:], 16)
|
71 |
+
for y in range(height):
|
72 |
+
r = int(r1 + (r2 - r1) * y / height)
|
73 |
+
g = int(g1 + (g2 - g1) * y / height)
|
74 |
+
b = int(b1 + (b2 - b1) * y / height)
|
75 |
+
draw.line([(0, y), (width, y)], fill=(r, g, b))
|
76 |
|
77 |
+
# Add emojis
|
78 |
+
try:
|
79 |
+
font = ImageFont.truetype("arial.ttf", 40)
|
80 |
+
except IOError:
|
81 |
+
font = ImageFont.load_default()
|
82 |
+
w, h = draw.textsize(shortcut['emojis'], font=font)
|
83 |
+
draw.text(((width - w) / 2, (height - h) / 4), shortcut['emojis'], font=font, fill='white')
|
|
|
84 |
|
85 |
+
# Add name
|
86 |
+
try:
|
87 |
+
font = ImageFont.truetype("arial.ttf", 24)
|
88 |
+
except IOError:
|
89 |
+
font = ImageFont.load_default()
|
90 |
+
w, h = draw.textsize(shortcut['name'], font=font)
|
91 |
+
draw.text(((width - w) / 2, (height - h) / 2), shortcut['name'], font=font, fill='white')
|
92 |
+
|
93 |
+
# Add short description
|
94 |
+
try:
|
95 |
+
font = ImageFont.truetype("arial.ttf", 16)
|
96 |
+
except IOError:
|
97 |
+
font = ImageFont.load_default()
|
98 |
+
w, h = draw.textsize(shortcut['short_description'], font=font)
|
99 |
+
draw.text(((width - w) / 2, (height - h) * 3 / 4), shortcut['short_description'], font=font, fill='white')
|
100 |
+
|
101 |
+
# Return image
|
102 |
+
return image
|
103 |
+
|
104 |
+
def update_display(sort_by='Recently Added', search_query='', filter_tags=[]):
|
105 |
+
datafra = get_shortcuts_dataframe(sort_by, search_query, filter_tags)
|
106 |
+
images = []
|
107 |
+
labels = []
|
108 |
+
for idx, shortcut in datafra.iterrows():
|
109 |
+
image = generate_shortcut_image(shortcut)
|
110 |
+
# Save image to buffer
|
111 |
+
buffer = io.BytesIO()
|
112 |
+
image.save(buffer, format='PNG')
|
113 |
+
buffer.seek(0)
|
114 |
+
images.append(buffer)
|
115 |
+
labels.append(shortcut['name'])
|
116 |
+
return images
|
117 |
|
118 |
load_shortcuts()
|
119 |
|
|
|
120 |
with gr.Blocks() as app:
|
121 |
gr.Markdown("## Add a New Website Shortcut")
|
122 |
with gr.Row():
|
|
|
130 |
color_to = gr.ColorPicker(label="Gradient Color To")
|
131 |
short_description = gr.Textbox(label="Short Description")
|
132 |
add_button = gr.Button("Add Shortcut")
|
|
|
|
|
|
|
133 |
add_button.click(
|
134 |
add_shortcut,
|
135 |
inputs=[name, tags, link, emojis, color_from, color_to, short_description],
|
136 |
+
outputs=[]
|
137 |
)
|
138 |
|
139 |
gr.Markdown("## Website Shortcuts")
|
140 |
with gr.Row():
|
141 |
search_bar = gr.Textbox(label="Search")
|
142 |
sort_options = gr.Dropdown(choices=['Recently Added', 'Alphabetical'], label="Sort By", value='Recently Added')
|
143 |
+
all_tags = set(tag for shortcut in shortcuts_list for tag in shortcut['tags'])
|
144 |
+
filter_tags = gr.CheckboxGroup(choices=list(all_tags), label="Filter Tags")
|
|
|
145 |
|
146 |
+
# Placeholder for the gallery
|
147 |
+
grid_output = gr.Gallery(
|
148 |
+
label="Shortcuts Gallery", show_label=False, elem_id="gallery",
|
149 |
+
columns=3, object_fit="contain", height="auto"
|
150 |
+
)
|
151 |
+
|
152 |
+
# Update the display whenever filters change
|
153 |
+
def refresh_display():
|
154 |
+
return update_display(sort_options.value, search_bar.value, filter_tags.value)
|
155 |
+
|
156 |
+
search_bar.change(fn=refresh_display, inputs=[], outputs=grid_output)
|
157 |
+
sort_options.change(fn=refresh_display, inputs=[], outputs=grid_output)
|
158 |
+
filter_tags.change(fn=refresh_display, inputs=[], outputs=grid_output)
|
159 |
+
|
160 |
+
# Initial display
|
161 |
+
grid_output.value = update_display()
|
162 |
+
|
163 |
+
# Variables to hold selected shortcut info
|
164 |
+
selected_index = gr.Number(visible=False)
|
165 |
+
shortcut_details = gr.Markdown(visible=False)
|
166 |
+
|
167 |
+
# Handle gallery selection
|
168 |
+
def gallery_select(evt: gr.SelectData):
|
169 |
+
index = evt.index
|
170 |
+
datafra = get_shortcuts_dataframe(sort_options.value, search_bar.value, filter_tags.value)
|
171 |
+
if index < len(datafra):
|
172 |
+
shortcut = datafra.iloc[index]
|
173 |
+
# Display shortcut details
|
174 |
+
shortcut_info = f"""
|
175 |
+
### {shortcut['name']} {shortcut['emojis']}
|
176 |
+
{shortcut['short_description']}
|
177 |
+
"""
|
178 |
+
return index, shortcut_info
|
179 |
+
else:
|
180 |
+
return None, None
|
181 |
+
|
182 |
+
grid_output.select(fn=gallery_select, inputs=None, outputs=[selected_index, shortcut_details])
|
183 |
+
shortcut_details.visible = True
|
184 |
+
|
185 |
+
# Buttons for interactivity
|
186 |
+
with gr.Row(visible=False) as action_row:
|
187 |
pin_button = gr.Button("π Pin/Unpin")
|
188 |
fav_button = gr.Button("β€οΈ Favorite/Unfavorite")
|
189 |
open_link_button = gr.Button("π Open Link")
|
190 |
+
|
191 |
+
# Show action buttons when a shortcut is selected
|
192 |
+
def show_actions(index):
|
193 |
+
if index is not None:
|
194 |
+
return gr.update(visible=True)
|
195 |
+
else:
|
196 |
+
return gr.update(visible=False)
|
197 |
+
|
198 |
+
selected_index.change(fn=show_actions, inputs=selected_index, outputs=action_row)
|
199 |
+
|
200 |
+
# Functions for buttons
|
201 |
+
def pin_action(index):
|
202 |
+
index = int(index)
|
203 |
+
datafra = get_shortcuts_dataframe(sort_options.value, search_bar.value, filter_tags.value)
|
204 |
+
if index < len(datafra):
|
205 |
+
shortcut = datafra.iloc[index]
|
206 |
+
# Find the original index in shortcuts_list
|
207 |
+
original_index = shortcuts_list.index(shortcut)
|
208 |
+
shortcuts_list[original_index]['pinned'] = not shortcuts_list[original_index]['pinned']
|
209 |
+
save_shortcuts()
|
210 |
+
return refresh_display(), gr.update(visible=False), gr.update(visible=False)
|
211 |
+
|
212 |
+
def favorite_action(index):
|
213 |
+
index = int(index)
|
214 |
+
datafra = get_shortcuts_dataframe(sort_options.value, search_bar.value, filter_tags.value)
|
215 |
+
if index < len(datafra):
|
216 |
+
shortcut = datafra.iloc[index]
|
217 |
+
# Find the original index in shortcuts_list
|
218 |
+
original_index = shortcuts_list.index(shortcut)
|
219 |
+
shortcuts_list[original_index]['favorited'] = not shortcuts_list[original_index]['favorited']
|
220 |
+
save_shortcuts()
|
221 |
+
return refresh_display(), gr.update(visible=False), gr.update(visible=False)
|
222 |
+
|
223 |
+
def open_link_action(index):
|
224 |
+
index = int(index)
|
225 |
+
datafra = get_shortcuts_dataframe(sort_options.value, search_bar.value, filter_tags.value)
|
226 |
+
if index < len(datafra):
|
227 |
+
shortcut = datafra.iloc[index]
|
228 |
+
return shortcut['link']
|
229 |
+
else:
|
230 |
+
return None
|
231 |
+
|
232 |
+
pin_button.click(fn=pin_action, inputs=selected_index, outputs=[grid_output, action_row, shortcut_details])
|
233 |
+
fav_button.click(fn=favorite_action, inputs=selected_index, outputs=[grid_output, action_row, shortcut_details])
|
234 |
+
open_link_button.click(fn=open_link_action, inputs=selected_index, outputs=None, _js="(link) => { if (link) { window.open(link, '_blank'); } }")
|
235 |
|
236 |
app.launch()
|