Update app.py
Browse files
app.py
CHANGED
@@ -1,220 +1,233 @@
|
|
1 |
-
from fastapi import FastAPI, Request
|
2 |
-
from fastapi.responses import HTMLResponse
|
3 |
-
from fastapi.middleware.cors import CORSMiddleware
|
4 |
import json
|
5 |
-
import
|
|
|
|
|
|
|
6 |
|
7 |
app = FastAPI()
|
8 |
-
DATA_FILE = "data.json"
|
9 |
-
|
10 |
-
# Allow CORS for frontend fetch if needed
|
11 |
-
app.add_middleware(
|
12 |
-
CORSMiddleware,
|
13 |
-
allow_origins=["*"],
|
14 |
-
allow_methods=["*"],
|
15 |
-
allow_headers=["*"],
|
16 |
-
)
|
17 |
-
|
18 |
-
# Initialize data file
|
19 |
-
if not os.path.exists(DATA_FILE):
|
20 |
-
with open(DATA_FILE, "w") as f:
|
21 |
-
json.dump([], f)
|
22 |
-
|
23 |
-
@app.post("/store")
|
24 |
-
async def store(request: Request):
|
25 |
-
data = await request.json()
|
26 |
-
with open(DATA_FILE, "w") as f:
|
27 |
-
json.dump(data, f)
|
28 |
-
return {"status": "success"}
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
@app.get("/", response_class=HTMLResponse)
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
<
|
38 |
-
<
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
<
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import json
|
2 |
+
from fastapi import FastAPI, HTTPException
|
3 |
+
import gradio as gr
|
4 |
+
from pydantic import BaseModel
|
5 |
+
from fastapi.responses import HTMLResponse
|
6 |
|
7 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
# Define a model for the incoming request data
|
10 |
+
class DataRequest(BaseModel):
|
11 |
+
list_name: str
|
12 |
+
items: list
|
13 |
+
|
14 |
+
# Store the lists data
|
15 |
+
lists = {}
|
16 |
+
|
17 |
+
# FastAPI endpoint for POST request to store data
|
18 |
+
@app.post("/store_data")
|
19 |
+
async def store_data(data: DataRequest):
|
20 |
+
list_name = data.list_name
|
21 |
+
items = data.items
|
22 |
+
|
23 |
+
if list_name not in lists:
|
24 |
+
lists[list_name] = []
|
25 |
+
|
26 |
+
lists[list_name].append(items)
|
27 |
+
return {"message": f"Data stored successfully for {list_name}"}
|
28 |
+
|
29 |
+
# FastAPI default root to serve custom HTML
|
30 |
@app.get("/", response_class=HTMLResponse)
|
31 |
+
def read_root():
|
32 |
+
return """
|
33 |
+
<!DOCTYPE html>
|
34 |
+
<html lang="en">
|
35 |
+
<head>
|
36 |
+
<meta charset="UTF-8" />
|
37 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
38 |
+
<title>UniShare</title>
|
39 |
+
<style>
|
40 |
+
body {
|
41 |
+
font-family: Arial, sans-serif;
|
42 |
+
margin: 0;
|
43 |
+
padding: 0;
|
44 |
+
background: #f0f2f5;
|
45 |
+
}
|
46 |
+
header {
|
47 |
+
background: #3b82f6;
|
48 |
+
color: white;
|
49 |
+
padding: 1em;
|
50 |
+
text-align: center;
|
51 |
+
font-size: 1.8em;
|
52 |
+
}
|
53 |
+
#overview {
|
54 |
+
display: grid;
|
55 |
+
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
56 |
+
gap: 1em;
|
57 |
+
padding: 1em;
|
58 |
+
}
|
59 |
+
.list-tile {
|
60 |
+
background: white;
|
61 |
+
padding: 1em;
|
62 |
+
border-radius: 10px;
|
63 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
64 |
+
cursor: pointer;
|
65 |
+
transition: transform 0.2s;
|
66 |
+
}
|
67 |
+
.list-tile:hover {
|
68 |
+
transform: scale(1.02);
|
69 |
+
}
|
70 |
+
#list-view {
|
71 |
+
display: none;
|
72 |
+
padding: 1em;
|
73 |
+
}
|
74 |
+
.back-button {
|
75 |
+
background: #3b82f6;
|
76 |
+
color: white;
|
77 |
+
padding: 0.5em 1em;
|
78 |
+
border: none;
|
79 |
+
border-radius: 5px;
|
80 |
+
cursor: pointer;
|
81 |
+
margin-bottom: 1em;
|
82 |
+
}
|
83 |
+
.card {
|
84 |
+
background: white;
|
85 |
+
padding: 1em;
|
86 |
+
border-radius: 10px;
|
87 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
88 |
+
margin-bottom: 1em;
|
89 |
+
}
|
90 |
+
.submenu {
|
91 |
+
margin-top: 1em;
|
92 |
+
}
|
93 |
+
.submenu h4 {
|
94 |
+
margin: 0.5em 0;
|
95 |
+
}
|
96 |
+
.grid {
|
97 |
+
display: grid;
|
98 |
+
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
|
99 |
+
gap: 1em;
|
100 |
+
}
|
101 |
+
.item {
|
102 |
+
background: #f9f9f9;
|
103 |
+
padding: 1em;
|
104 |
+
border-radius: 10px;
|
105 |
+
box-shadow: 0 1px 3px rgba(0,0,0,0.05);
|
106 |
+
}
|
107 |
+
.item img {
|
108 |
+
max-width: 100%;
|
109 |
+
max-height: 150px;
|
110 |
+
object-fit: cover;
|
111 |
+
border-radius: 5px;
|
112 |
+
margin-bottom: 0.5em;
|
113 |
+
}
|
114 |
+
.item a {
|
115 |
+
display: inline-block;
|
116 |
+
margin-top: 0.5em;
|
117 |
+
text-decoration: none;
|
118 |
+
color: #3b82f6;
|
119 |
+
}
|
120 |
+
#toast {
|
121 |
+
position: fixed;
|
122 |
+
bottom: 20px;
|
123 |
+
right: 20px;
|
124 |
+
background: #333;
|
125 |
+
color: white;
|
126 |
+
padding: 1em;
|
127 |
+
border-radius: 5px;
|
128 |
+
display: none;
|
129 |
+
z-index: 1000;
|
130 |
+
}
|
131 |
+
</style>
|
132 |
+
</head>
|
133 |
+
<body>
|
134 |
+
<header>UniShare</header>
|
135 |
+
<div id="overview"></div>
|
136 |
+
<div id="list-view">
|
137 |
+
<button class="back-button" onclick="backToOverview()">← Back to Overview</button>
|
138 |
+
<div id="content"></div>
|
139 |
+
</div>
|
140 |
+
<div id="toast"></div>
|
141 |
+
|
142 |
+
<script>
|
143 |
+
const overviewDiv = document.getElementById('overview');
|
144 |
+
const contentDiv = document.getElementById('content');
|
145 |
+
const listViewDiv = document.getElementById('list-view');
|
146 |
+
const toastDiv = document.getElementById('toast');
|
147 |
+
|
148 |
+
const lists = {}
|
149 |
+
|
150 |
+
function showToast(message) {
|
151 |
+
toastDiv.textContent = message;
|
152 |
+
toastDiv.style.display = 'block';
|
153 |
+
setTimeout(() => {
|
154 |
+
toastDiv.style.display = 'none';
|
155 |
+
}, 3000);
|
156 |
+
}
|
157 |
+
|
158 |
+
function backToOverview() {
|
159 |
+
listViewDiv.style.display = 'none';
|
160 |
+
overviewDiv.style.display = 'grid';
|
161 |
+
}
|
162 |
+
|
163 |
+
function showList(name) {
|
164 |
+
const sections = lists[name];
|
165 |
+
contentDiv.innerHTML = '';
|
166 |
+
|
167 |
+
sections.forEach(section => {
|
168 |
+
const card = document.createElement('div');
|
169 |
+
card.className = 'card';
|
170 |
+
const title = document.createElement('h3');
|
171 |
+
title.textContent = section.sectionTitle;
|
172 |
+
card.appendChild(title);
|
173 |
+
|
174 |
+
const grid = document.createElement('div');
|
175 |
+
grid.className = 'grid';
|
176 |
+
|
177 |
+
section.items.forEach(item => {
|
178 |
+
const div = document.createElement('div');
|
179 |
+
div.className = 'item';
|
180 |
+
let content = `<strong>${item.title}</strong><br>`;
|
181 |
+
if (item.link.match(/\.(jpeg|jpg|gif|png)$/)) {
|
182 |
+
content += `<img src="${item.link}" alt="${item.title}" />`;
|
183 |
+
}
|
184 |
+
content += `<a href="${item.link}" download>Download</a>`;
|
185 |
+
div.innerHTML = content;
|
186 |
+
grid.appendChild(div);
|
187 |
+
});
|
188 |
+
|
189 |
+
card.appendChild(grid);
|
190 |
+
contentDiv.appendChild(card);
|
191 |
+
});
|
192 |
+
|
193 |
+
overviewDiv.style.display = 'none';
|
194 |
+
listViewDiv.style.display = 'block';
|
195 |
+
}
|
196 |
+
|
197 |
+
// Fetch data and populate the lists dynamically
|
198 |
+
fetch('/gradio/data')
|
199 |
+
.then(response => response.json())
|
200 |
+
.then(data => {
|
201 |
+
if (Object.keys(data).length > 0) {
|
202 |
+
Object.keys(data).forEach((listName, index) => {
|
203 |
+
const list = data[listName];
|
204 |
+
const tile = document.createElement('div');
|
205 |
+
tile.className = 'list-tile';
|
206 |
+
tile.innerHTML = `<strong>${listName}</strong><br>${list.length} sections`;
|
207 |
+
tile.onclick = () => showList(listName);
|
208 |
+
overviewDiv.appendChild(tile);
|
209 |
+
showToast(`Loaded ${listName}`);
|
210 |
+
});
|
211 |
+
} else {
|
212 |
+
overviewDiv.innerHTML = '<p>No lists found</p>';
|
213 |
+
}
|
214 |
+
});
|
215 |
+
</script>
|
216 |
+
</body>
|
217 |
+
</html>
|
218 |
+
"""
|
219 |
+
|
220 |
+
# Gradio Interface for displaying the stored data
|
221 |
+
def show_data():
|
222 |
+
return lists
|
223 |
+
|
224 |
+
# Gradio app interface
|
225 |
+
gr_interface = gr.Interface(fn=show_data, live=True)
|
226 |
+
|
227 |
+
# Add the Gradio app to FastAPI
|
228 |
+
app = gr.mount_gradio_app(app, gr_interface, path="/gradio")
|
229 |
+
|
230 |
+
# FastAPI default root (e.g. for status or basic testing)
|
231 |
+
@app.get("/gradio/data")
|
232 |
+
async def get_data():
|
233 |
+
return lists
|