Files
flask-reports/app.py

50 lines
1.5 KiB
Python

import requests, os
import plotly.graph_objects as go
import plotly.express as px
from flask import Flask, render_template, jsonify, make_response
from weasyprint import HTML
app = Flask(__name__)
@app.route('/report')
def report():
# Create Plotly graph
#fig = go.Figure(data=[go.Scatter(x=[1, 2, 3], y=[10, 20, 30], mode='lines')])
df = px.data.stocks()
fig = px.line(df, x='date', y=['AAPL', 'GOOG'])
# 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, table_data = df)
@app.route('/')
def home():
return render_template('base.html')
def get_data():
response = requests.get('https://api.example.com/data')
return response.json()
@app.route('/download-pdf')
def download_pdf():
# 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'
return response
if __name__ == '__main__':
app.run(debug=True)