adjusted graph to be svg

This commit is contained in:
Nico Melone
2025-01-22 16:11:17 -06:00
parent 9645bb4170
commit 0c6098a011
7 changed files with 100 additions and 35 deletions

36
app.py
View File

@@ -1,4 +1,5 @@
import requests
import requests, os
import plotly.graph_objects as go
from flask import Flask, render_template, jsonify, make_response
from weasyprint import HTML
@@ -6,15 +7,17 @@ app = Flask(__name__)
@app.route('/report')
def report():
# Prepare graph data
graph_data = [
{
'x': [1, 2, 3],
'y': [10, 20, 30],
'type': 'scatter'
}
]
return render_template('report.html', graph_data=jsonify(graph_data))
# Create Plotly graph
fig = go.Figure(data=[go.Scatter(x=[1, 2, 3], y=[10, 20, 30], mode='lines')])
# Save the graph as an image
image_path = os.path.join('static', 'graph.svg')
fig.write_image(image_path)
return render_template('report.html', graph_image=image_path)
@app.route('/')
def home():
return render_template('base.html')
def get_data():
response = requests.get('https://api.example.com/data')
@@ -22,8 +25,19 @@ def get_data():
@app.route('/download-pdf')
def download_pdf():
html = render_template('report.html')
# Create Plotly graph
fig = go.Figure(data=[go.Scatter(x=[1, 2, 3], y=[10, 20, 30], mode='lines')])
# Save the graph as an image
this_folder = os.path.dirname(os.path.abspath(__file__))
image_path = os.path.join(this_folder, 'static', 'graph.svg')
fig.write_image(image_path)
# Render the HTML template with the graph image
html = render_template('report.html', graph_image="file://" + image_path)
print(html)
pdf = HTML(string=html).write_pdf()
response = make_response(pdf)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = 'inline; filename=report.pdf'