diff --git a/app.py b/app.py index 5d0b32a..1ad5c28 100644 --- a/app.py +++ b/app.py @@ -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' diff --git a/requirements.txt b/requirements.txt index 3744fe6..ca2a054 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ flask requests weasyprint -plotly \ No newline at end of file +plotly +kaleido \ No newline at end of file diff --git a/static/css/styles.css b/static/css/styles.css new file mode 100644 index 0000000..a36931d --- /dev/null +++ b/static/css/styles.css @@ -0,0 +1,36 @@ +@page { + size: Letter; + margin: 1in; +} + +body { + font-family: Arial, sans-serif; + margin: 0; + padding: 0; +} + +table { + width: 100%; + border-collapse: collapse; +} + +th, td { + border: 1px solid #ddd; + padding: 8px; + text-align: left; +} + +th { + background-color: #f4f4f4; +} + +.graph-container { + width: 100%; + max-height: 400px; + overflow: hidden; + margin-bottom: 20px; +} + +.page-break { + page-break-before: always; +} \ No newline at end of file diff --git a/static/graph.png b/static/graph.png new file mode 100644 index 0000000..a032588 Binary files /dev/null and b/static/graph.png differ diff --git a/static/graph.svg b/static/graph.svg new file mode 100644 index 0000000..6114cf6 --- /dev/null +++ b/static/graph.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..c6a2238 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,12 @@ + + +
+ + +| Header 1 | -Header 2 | +Header 1 | +Header 2 |
|---|---|---|---|
| {{ row[0] }} | -{{ row[1] }} | -||
| {{ row[0] }} | +{{ row[1] }} | +