Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,82 +6,149 @@ from fastapi import FastAPI
|
|
6 |
# Create a FastAPI app
|
7 |
app = FastAPI()
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
<!DOCTYPE html>
|
14 |
<html>
|
15 |
<head>
|
16 |
<meta charset="utf-8">
|
17 |
-
<title>D3.js
|
18 |
<script src="https://d3js.org/d3.v7.min.js"></script>
|
19 |
<style>
|
20 |
-
body {
|
21 |
font-family: sans-serif;
|
22 |
padding: 20px;
|
23 |
-
background-color: #
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
}
|
30 |
-
.
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
}
|
|
|
|
|
|
|
|
|
40 |
</style>
|
41 |
</head>
|
42 |
<body>
|
43 |
-
<h1>
|
44 |
-
<div id="
|
45 |
|
46 |
<script>
|
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 |
</script>
|
86 |
</body>
|
87 |
</html>
|
@@ -89,22 +156,23 @@ async def serve_test_page():
|
|
89 |
|
90 |
return fastapi.responses.HTMLResponse(content=html_content)
|
91 |
|
92 |
-
# Create a simple Gradio interface with an iframe
|
93 |
def create_iframe():
|
94 |
# Add a random parameter to force reload
|
95 |
random_param = random.randint(1, 10000)
|
96 |
-
return f'<iframe src="/d3-
|
97 |
|
98 |
# Create the Gradio blocks
|
99 |
-
with gr.Blocks() as demo:
|
100 |
-
gr.Markdown("#
|
|
|
101 |
iframe_output = gr.HTML(create_iframe())
|
102 |
|
103 |
-
# Refresh button
|
104 |
def refresh():
|
105 |
return create_iframe()
|
106 |
|
107 |
-
gr.Button("
|
108 |
|
109 |
# Mount the Gradio app to the FastAPI app
|
110 |
gr.mount_gradio_app(app, demo, path="/")
|
|
|
6 |
# Create a FastAPI app
|
7 |
app = FastAPI()
|
8 |
|
9 |
+
# Sample data for the plot
|
10 |
+
def generate_data():
|
11 |
+
return [
|
12 |
+
{"name": "Argentina", "value": random.randint(10, 90)},
|
13 |
+
{"name": "Brazil", "value": random.randint(10, 90)},
|
14 |
+
{"name": "Chile", "value": random.randint(10, 90)},
|
15 |
+
{"name": "Colombia", "value": random.randint(10, 90)},
|
16 |
+
{"name": "Mexico", "value": random.randint(10, 90)},
|
17 |
+
{"name": "Peru", "value": random.randint(10, 90)},
|
18 |
+
{"name": "Spain", "value": random.randint(10, 90)},
|
19 |
+
{"name": "Venezuela", "value": random.randint(10, 90)}
|
20 |
+
]
|
21 |
+
|
22 |
+
# Route to serve a D3.js bar chart
|
23 |
+
@app.get("/d3-plot")
|
24 |
+
async def serve_plot():
|
25 |
+
# Generate random data for the plot
|
26 |
+
data = generate_data()
|
27 |
+
|
28 |
+
# Convert the data to a JavaScript string
|
29 |
+
data_str = str(data).replace("'", '"')
|
30 |
+
|
31 |
+
html_content = f"""
|
32 |
<!DOCTYPE html>
|
33 |
<html>
|
34 |
<head>
|
35 |
<meta charset="utf-8">
|
36 |
+
<title>D3.js Simple Plot</title>
|
37 |
<script src="https://d3js.org/d3.v7.min.js"></script>
|
38 |
<style>
|
39 |
+
body {{
|
40 |
font-family: sans-serif;
|
41 |
padding: 20px;
|
42 |
+
background-color: #111;
|
43 |
+
color: #fff;
|
44 |
+
}}
|
45 |
+
.bar {{
|
46 |
+
fill: #f32b7b;
|
47 |
+
transition: fill 0.3s;
|
48 |
+
}}
|
49 |
+
.bar:hover {{
|
50 |
+
fill: #4a1942;
|
51 |
+
}}
|
52 |
+
.axis text {{
|
53 |
+
fill: #ccc;
|
54 |
+
}}
|
55 |
+
.axis path,
|
56 |
+
.axis line {{
|
57 |
+
stroke: #666;
|
58 |
+
}}
|
59 |
+
.axis-title {{
|
60 |
+
fill: #fff;
|
61 |
+
font-size: 12px;
|
62 |
+
}}
|
63 |
</style>
|
64 |
</head>
|
65 |
<body>
|
66 |
+
<h1>Latin America & Spain Progress</h1>
|
67 |
+
<div id="chart"></div>
|
68 |
|
69 |
<script>
|
70 |
+
// The data from Python
|
71 |
+
const data = {data_str};
|
72 |
+
|
73 |
+
document.addEventListener('DOMContentLoaded', function() {{
|
74 |
+
// Set up dimensions
|
75 |
+
const margin = {{top: 30, right: 30, bottom: 70, left: 60}};
|
76 |
+
const width = 800 - margin.left - margin.right;
|
77 |
+
const height = 400 - margin.top - margin.bottom;
|
78 |
+
|
79 |
+
// Create SVG
|
80 |
+
const svg = d3.select("#chart")
|
81 |
+
.append("svg")
|
82 |
+
.attr("width", width + margin.left + margin.right)
|
83 |
+
.attr("height", height + margin.top + margin.bottom)
|
84 |
+
.append("g")
|
85 |
+
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
86 |
+
|
87 |
+
// X axis
|
88 |
+
const x = d3.scaleBand()
|
89 |
+
.range([0, width])
|
90 |
+
.domain(data.map(function(d) {{ return d.name; }}))
|
91 |
+
.padding(0.2);
|
92 |
+
|
93 |
+
svg.append("g")
|
94 |
+
.attr("class", "axis")
|
95 |
+
.attr("transform", "translate(0," + height + ")")
|
96 |
+
.call(d3.axisBottom(x))
|
97 |
+
.selectAll("text")
|
98 |
+
.attr("transform", "translate(-10,0)rotate(-45)")
|
99 |
+
.style("text-anchor", "end");
|
100 |
|
101 |
+
// Add X axis title
|
102 |
+
svg.append("text")
|
103 |
+
.attr("class", "axis-title")
|
104 |
+
.attr("text-anchor", "middle")
|
105 |
+
.attr("x", width / 2)
|
106 |
+
.attr("y", height + margin.bottom - 5)
|
107 |
+
.text("Country");
|
108 |
+
|
109 |
+
// Y axis
|
110 |
+
const y = d3.scaleLinear()
|
111 |
+
.domain([0, 100])
|
112 |
+
.range([height, 0]);
|
113 |
+
|
114 |
+
svg.append("g")
|
115 |
+
.attr("class", "axis")
|
116 |
+
.call(d3.axisLeft(y));
|
117 |
+
|
118 |
+
// Add Y axis title
|
119 |
+
svg.append("text")
|
120 |
+
.attr("class", "axis-title")
|
121 |
+
.attr("text-anchor", "middle")
|
122 |
+
.attr("transform", "rotate(-90)")
|
123 |
+
.attr("x", -height / 2)
|
124 |
+
.attr("y", -margin.left + 15)
|
125 |
+
.text("Progress (%)");
|
126 |
+
|
127 |
+
// Bars
|
128 |
+
svg.selectAll("rect")
|
129 |
+
.data(data)
|
130 |
+
.enter()
|
131 |
+
.append("rect")
|
132 |
+
.attr("class", "bar")
|
133 |
+
.attr("x", function(d) {{ return x(d.name); }})
|
134 |
+
.attr("y", function(d) {{ return y(d.value); }})
|
135 |
+
.attr("width", x.bandwidth())
|
136 |
+
.attr("height", function(d) {{ return height - y(d.value); }})
|
137 |
+
.append("title")
|
138 |
+
.text(function(d) {{ return d.name + ": " + d.value + "%"; }});
|
139 |
+
|
140 |
+
// Value labels on top of bars
|
141 |
+
svg.selectAll(".label")
|
142 |
+
.data(data)
|
143 |
+
.enter()
|
144 |
+
.append("text")
|
145 |
+
.attr("class", "label")
|
146 |
+
.attr("text-anchor", "middle")
|
147 |
+
.attr("x", function(d) {{ return x(d.name) + x.bandwidth() / 2; }})
|
148 |
+
.attr("y", function(d) {{ return y(d.value) - 5; }})
|
149 |
+
.text(function(d) {{ return d.value + "%"; }})
|
150 |
+
.attr("fill", "white");
|
151 |
+
}});
|
152 |
</script>
|
153 |
</body>
|
154 |
</html>
|
|
|
156 |
|
157 |
return fastapi.responses.HTMLResponse(content=html_content)
|
158 |
|
159 |
+
# Create a simple Gradio interface with an iframe to display the plot
|
160 |
def create_iframe():
|
161 |
# Add a random parameter to force reload
|
162 |
random_param = random.randint(1, 10000)
|
163 |
+
return f'<iframe src="/d3-plot?t={random_param}" style="width:100%; height:500px; border:none;"></iframe>'
|
164 |
|
165 |
# Create the Gradio blocks
|
166 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="pink", secondary_hue="purple")) as demo:
|
167 |
+
gr.Markdown("# Latin America & Spain Progress Chart")
|
168 |
+
|
169 |
iframe_output = gr.HTML(create_iframe())
|
170 |
|
171 |
+
# Refresh button to generate new random data
|
172 |
def refresh():
|
173 |
return create_iframe()
|
174 |
|
175 |
+
gr.Button("Generate New Data").click(fn=refresh, outputs=iframe_output)
|
176 |
|
177 |
# Mount the Gradio app to the FastAPI app
|
178 |
gr.mount_gradio_app(app, demo, path="/")
|