ouhenio commited on
Commit
db58fdb
·
verified ·
1 Parent(s): dcf465d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -68
app.py CHANGED
@@ -6,82 +6,149 @@ from fastapi import FastAPI
6
  # Create a FastAPI app
7
  app = FastAPI()
8
 
9
- # Route to serve a simple D3.js test page
10
- @app.get("/d3-test")
11
- async def serve_test_page():
12
- html_content = """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  <!DOCTYPE html>
14
  <html>
15
  <head>
16
  <meta charset="utf-8">
17
- <title>D3.js Load Test</title>
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: #f5f5f5;
24
- }
25
- #status {
26
- margin-top: 20px;
27
- padding: 15px;
28
- border-radius: 5px;
29
- }
30
- .success {
31
- background-color: #d4edda;
32
- color: #155724;
33
- border: 1px solid #c3e6cb;
34
- }
35
- .error {
36
- background-color: #f8d7da;
37
- color: #721c24;
38
- border: 1px solid #f5c6cb;
39
- }
 
 
 
 
40
  </style>
41
  </head>
42
  <body>
43
- <h1>D3.js Load Test</h1>
44
- <div id="status">Checking if D3.js is loaded...</div>
45
 
46
  <script>
47
- document.addEventListener('DOMContentLoaded', function() {
48
- const statusDiv = document.getElementById('status');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- try {
51
- // Check if d3 is defined
52
- if (typeof d3 !== 'undefined') {
53
- statusDiv.textContent = 'D3.js successfully loaded! (Version: ' + d3.version + ')';
54
- statusDiv.className = 'success';
55
-
56
- // Create a simple SVG to verify D3 functions work
57
- const svg = d3.select('body')
58
- .append('svg')
59
- .attr('width', 200)
60
- .attr('height', 100);
61
-
62
- svg.append('circle')
63
- .attr('cx', 50)
64
- .attr('cy', 50)
65
- .attr('r', 40)
66
- .style('fill', 'steelblue');
67
-
68
- svg.append('text')
69
- .attr('x', 50)
70
- .attr('y', 50)
71
- .attr('text-anchor', 'middle')
72
- .attr('dominant-baseline', 'middle')
73
- .text('D3')
74
- .style('fill', 'white');
75
- } else {
76
- statusDiv.textContent = 'Error: D3.js is not loaded';
77
- statusDiv.className = 'error';
78
- }
79
- } catch (error) {
80
- statusDiv.textContent = 'Error: ' + error.message;
81
- statusDiv.className = 'error';
82
- console.error('D3 test error:', error);
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-test?t={random_param}" style="width:100%; height:300px; border:none;"></iframe>'
97
 
98
  # Create the Gradio blocks
99
- with gr.Blocks() as demo:
100
- gr.Markdown("# D3.js Load Test")
 
101
  iframe_output = gr.HTML(create_iframe())
102
 
103
- # Refresh button
104
  def refresh():
105
  return create_iframe()
106
 
107
- gr.Button("Refresh Test").click(fn=refresh, outputs=iframe_output)
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="/")