charbelmalo commited on
Commit
7e9fa0a
·
1 Parent(s): 4a98ed4
Files changed (1) hide show
  1. app.py +47 -215
app.py CHANGED
@@ -1,217 +1,49 @@
1
  import gradio as gr
2
- import json
3
- import os
4
-
5
- from gradio_modal import Modal
6
- # File to store data
7
- DATA_FILE = 'data.json'
8
-
9
- # Initialize data if file doesn't exist
10
- if not os.path.exists(DATA_FILE):
11
- with open(DATA_FILE, 'w') as f:
12
- json.dump({'spaces': [], 'tags': []}, f)
13
-
14
- # Helper functions to read and write data
15
- def load_data():
16
- with open(DATA_FILE, 'r') as f:
17
- return json.load(f)
18
-
19
- def save_data(data):
20
- with open(DATA_FILE, 'w') as f:
21
- json.dump(data, f, indent=4)
22
-
23
- # Function to display spaces
24
- def display_spaces(filter_tag=None, filter_status=None):
25
- data = load_data()
26
- spaces = data['spaces']
27
- tags = data['tags']
28
-
29
- # Apply filters if any
30
- if filter_tag:
31
- spaces = [s for s in spaces if filter_tag in s['tags']]
32
- if filter_status and filter_status != 'All':
33
- spaces = [s for s in spaces if s['status'] == filter_status]
34
-
35
- # Generate HTML for spaces
36
- html = ''
37
- for space in spaces:
38
- html += f'''
39
- <div style="border:1px solid #ccc; padding:10px; margin:10px; width:300px; display:inline-block; vertical-align:top;">
40
- <h3>{space['name']}</h3>
41
- <p><strong>Status:</strong> {space['status']}</p>
42
- <p><strong>Tags:</strong> {', '.join(space['tags'])}</p>
43
- <p>{space['description']}</p>
44
- <a href="{space['link']}" target="_blank">Open Link</a><br><br>
45
- <button onclick="edit_space('{space['id']}')">Edit</button>
46
- <button onclick="delete_space('{space['id']}')">Delete</button>
47
- </div>
48
- '''
49
- if not html:
50
- html = '<p>No spaces available.</p>'
51
- return html
52
-
53
- # Function to add/edit a space
54
- def save_space(id, name, description, link, status, tags, parent_id):
55
- data = load_data()
56
- space = {
57
- 'id': id or str(len(data['spaces']) + 1),
58
- 'name': name,
59
- 'description': description,
60
- 'link': link,
61
- 'status': status,
62
- 'tags': tags.split(',') if tags else [],
63
- 'parent_id': parent_id
64
- }
65
- if id:
66
- # Edit existing space
67
- data['spaces'] = [s for s in data['spaces'] if s['id'] != id]
68
- data['spaces'].append(space)
69
- save_data(data)
70
- return 'Space saved successfully.'
71
-
72
- # Function to delete a space
73
- def delete_space(id):
74
- data = load_data()
75
- data['spaces'] = [s for s in data['spaces'] if s['id'] != id]
76
- save_data(data)
77
- return 'Space deleted successfully.'
78
-
79
- # Function to get space details for editing
80
- def get_space_details(id):
81
- data = load_data()
82
- space = next((s for s in data['spaces'] if s['id'] == id), None)
83
- if space:
84
- return space['name'], space['description'], space['link'], space['status'], ','.join(space['tags']), space['parent_id']
85
- return '', '', '', '', '', ''
86
-
87
- # Function to manage tags
88
- def add_tag(name, parent_name):
89
- data = load_data()
90
- tag = {
91
- 'name': name,
92
- 'parent_name': parent_name or None
93
  }
94
- data['tags'].append(tag)
95
- save_data(data)
96
- return 'Tag added successfully.'
97
-
98
- # Function to get list of tags
99
- def get_tags():
100
- data = load_data()
101
- return [tag['name'] for tag in data['tags']]
102
-
103
- # Main Gradio interface
104
- with gr.Blocks() as demo:
105
- gr.Markdown("# Spaces Management App")
106
-
107
- # Filters
108
- with gr.Row():
109
- filter_tag = gr.Dropdown(label='Filter by Tag', choices=[''] + get_tags())
110
- filter_status = gr.Dropdown(label='Filter by Status', choices=['All', 'Running', 'Error'])
111
-
112
- # Display spaces
113
- spaces_html = gr.HTML()
114
-
115
- def update_spaces(filter_tag, filter_status):
116
- return display_spaces(filter_tag, filter_status)
117
-
118
- filter_tag.change(fn=update_spaces, inputs=[filter_tag, filter_status], outputs=spaces_html)
119
- filter_status.change(fn=update_spaces, inputs=[filter_tag, filter_status], outputs=spaces_html)
120
-
121
- # Initial display
122
- spaces_html.value = display_spaces()
123
-
124
- # Add space button
125
- with gr.Row():
126
- add_space_btn = gr.Button("Add Space")
127
-
128
- # Modal for adding/editing space
129
- with Gr.Group(visible=True) as modal:
130
- space_id = gr.Textbox(visible=False)
131
- space_name = gr.Textbox(label='Space Name')
132
- space_description = gr.Textbox(label='Description', lines=3)
133
- space_link = gr.Textbox(label='Link')
134
- space_status = gr.Dropdown(label='Status', choices=['Running', 'Error'])
135
- space_tags = gr.Textbox(label='Tags (comma separated)')
136
- space_parent_id = gr.Textbox(label='Parent Space ID (optional)')
137
- save_space_btn = gr.Button("Save Space")
138
-
139
- def open_add_space():
140
- return gr.update(visible=True), '', '', '', '', '', ''
141
-
142
- add_space_btn.click(fn=open_add_space, outputs=[modal, space_id, space_name, space_description, space_link, space_status, space_tags])
143
-
144
- def submit_space(space_id, space_name, space_description, space_link, space_status, space_tags, space_parent_id):
145
- message = save_space(space_id, space_name, space_description, space_link, space_status, space_tags, space_parent_id)
146
- html = display_spaces()
147
- return gr.update(visible=False), html, gr.update(value=''), gr.update(value=''), gr.update(value=''), gr.update(value=''), gr.update(value=''), gr.update(value='')
148
-
149
- save_space_btn.click(fn=submit_space, inputs=[space_id, space_name, space_description, space_link, space_status, space_tags, space_parent_id],
150
- outputs=[modal, spaces_html, space_id, space_name, space_description, space_link, space_status, space_tags])
151
-
152
- # JavaScript functions for edit/delete
153
- gr.HTML('''
154
- <script>
155
- function edit_space(id) {
156
- fetch('/get_space_details', {
157
- method: 'POST',
158
- headers: {'Content-Type': 'application/json'},
159
- body: JSON.stringify({'id': id})
160
- }).then(response => response.json()).then(data => {
161
- space_id.value = id;
162
- space_name.value = data[0];
163
- space_description.value = data[1];
164
- space_link.value = data[2];
165
- space_status.value = data[3];
166
- space_tags.value = data[4];
167
- space_parent_id.value = data[5];
168
- modal.style.display = 'block';
169
- });
170
- }
171
-
172
- function delete_space(id) {
173
- if (confirm('Are you sure you want to delete this space?')) {
174
- fetch('/delete_space', {
175
- method: 'POST',
176
- headers: {'Content-Type': 'application/json'},
177
- body: JSON.stringify({'id': id})
178
- }).then(response => response.json()).then(data => {
179
- alert(data);
180
- location.reload();
181
- });
182
- }
183
- }
184
- </script>
185
- ''')
186
-
187
- # End of Gradio Blocks
188
-
189
- # Gradio routes for edit/delete functions
190
- from fastapi import FastAPI, Request
191
- import uvicorn
192
-
193
- io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox")
194
- app = gr.mount_gradio_app(demo, io, path="/gradio")
195
-
196
- @app.post("/get_space_details")
197
- async def get_space_details_route(request: Request):
198
- req = await request.json()
199
- id = req.get('id')
200
- details = get_space_details(id)
201
- return details
202
-
203
- @app.post("/delete_space")
204
- async def delete_space_route(request: Request):
205
- req = await request.json()
206
- id = req.get('id')
207
- message = delete_space(id)
208
- return message
209
-
210
- # Run the app
211
- def read_main():
212
- return {"message": "This is your main app"}
213
-
214
-
215
-
216
- if __name__ == "__main__":
217
- uvicorn.run(app, host="0.0.0.0")
 
1
  import gradio as gr
2
+ from datetime import datetime
3
+
4
+ shortcuts_list = []
5
+
6
+ def add_shortcut(name, tags, link, emojis, color_from, color_to, short_description):
7
+ new_shortcut = {
8
+ 'name': name.strip(),
9
+ 'tags': [tag.strip() for tag in tags.split('/')],
10
+ 'link': link.strip(),
11
+ 'emojis': emojis.strip(),
12
+ 'color_from': color_from,
13
+ 'color_to': color_to,
14
+ 'short_description': short_description.strip(),
15
+ 'pinned': False,
16
+ 'favorited': False,
17
+ 'date_added': datetime.now()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  }
19
+ shortcuts_list.append(new_shortcut)
20
+ return update_display()
21
+
22
+ def update_display(sort_by='date_added', search_query='', filter_tags=[]):
23
+ # For simplicity, just return the list length
24
+ return f"Total Shortcuts: {len(shortcuts_list)}"
25
+
26
+ with gr.Blocks() as app:
27
+ with gr.Column():
28
+ gr.Markdown("## Add a New Website Shortcut")
29
+ name = gr.Textbox(label="Name")
30
+ link = gr.Textbox(label="Link")
31
+ tags = gr.Textbox(label="Tags (separate levels with '/')")
32
+ emojis = gr.Textbox(label="Emojis")
33
+ color_from = gr.ColorPicker(label="Gradient Color From")
34
+ color_to = gr.ColorPicker(label="Gradient Color To")
35
+ short_description = gr.Textbox(label="Short Description")
36
+ add_button = gr.Button("Add Shortcut")
37
+ output = gr.Textbox(label="Status", interactive=False)
38
+
39
+ add_button.click(
40
+ add_shortcut,
41
+ inputs=[name, tags, link, emojis, color_from, color_to, short_description],
42
+ outputs=output
43
+ )
44
+
45
+ # Placeholder for display area
46
+ gr.Markdown("## Website Shortcuts")
47
+ # ... Display logic goes here ...
48
+
49
+ app.launch()