View
unknown
python
2 years ago
5.7 kB
3
Indexable
from io import BytesIO from django.shortcuts import render from flask import get_template_attribute from appmanager.models import Table, CategoryTable, Fields from django.http import FileResponse, JsonResponse, HttpResponse from django.core.paginator import Paginator from django_plotly_dash import DjangoDash import dash_html_components as html import dash_core_components as dcc from django.template.loader import render_to_string from xhtml2pdf import pisa from django.template.loader import get_template # from weasyprint import HTML import base64 from django.conf import settings from plotly.graph_objs import Figure from io import BytesIO from plotly.offline import plot import plotly.graph_objs as go import plotly.io as pio # import tempfile import os import random from django.templatetags.static import static from plotly.subplots import make_subplots import string from datetime import datetime def render_to_pdf(template_src, context_dict={}): template = get_template(template_src) html = template.render(context_dict) result = BytesIO() pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result) if not pdf.err: return HttpResponse(result.getvalue(), content_type='application/pdf') return None data = { "company": "Dennnis Ivanov Company", "address": "123 Street name", "city": "Vancouver", "state": "WA", "zipcode": "98663", "phone": "555-555-2345", "email": "youremail@dennisivy.com", "website": "dennisivy.com", } app_name = "SimpleExample" dash_app = DjangoDash(name=app_name) dash_app.layout = html.Div([ html.H1("My Plotly Dash App"), dcc.Graph( id='example-graph', figure={ 'data': [ {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'}, {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': 'Montreal'}, ], 'layout': { 'title': 'Dash Data Visualization' } } ), dcc.Graph( id='example-graph2', figure={ 'data': [ go.Pie( labels=['Label 1', 'Label 2', 'Label 3'], values=[45, 30, 25], hoverinfo='label+percent', textinfo='value', textfont=dict(size=20), marker=dict( colors=['rgb(255, 127, 14)', 'rgb(44, 160, 44)', 'rgb(31, 119, 180)'], line=dict(color='#ffffff', width=2) ), hole=0.5, ) ], 'layout': go.Layout( title='Pie Chart Example', showlegend=True, legend=dict( x=0.5, y=1.0, bgcolor='rgba(255, 255, 255, 0.5)', orientation='h' ), ) } ) ]) def generate_random_filename(length=15): letters = string.ascii_letters timestamp = datetime.now().strftime("%Y%m%d%H%M%S") random_string = ''.join(random.choice(letters) for _ in range(length)) return f"{timestamp}_{random_string}.png" def create_figure1(): # Create Figure 1 fig = go.Figure(data=[go.Scatter(x=[1, 2, 3], y=[4, 1, 2])]) fig.update_layout(title='Figure 1') return fig def create_figure2(): # Create Figure 2 fig = make_subplots(rows=1, cols=2) fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 4, 5], name='SF'), row=1, col=1) fig.add_trace(go.Scatter(x=[1, 2, 3], y=[5, 3, 1], name='Montreal'), row=1, col=2) fig.update_layout(title='Figure 2') return fig def create_figure3(): # Create Figure 3 - Pie Chart labels = ['Label 1', 'Label 2', 'Label 3'] values = [45, 30, 25] fig = go.Figure(data=[go.Pie(labels=labels, values=values)]) fig.update_layout(title='Figure 3 - Pie Chart') return fig def save_figure(fig, file_path): # Save the figure as a PNG file pio.write_image(fig, file_path) def download_charts_pdf(request): # Generate the figures fig1 = create_figure1() fig2 = create_figure2() fig3 = create_figure3() # Save the figures as PNG files filename1 = generate_random_filename() filename2 = generate_random_filename() filename3 = generate_random_filename() file_path1 = os.path.join(settings.MEDIA_ROOT, 'charts', filename1) file_path2 = os.path.join(settings.MEDIA_ROOT, 'charts', filename2) file_path3 = os.path.join(settings.MEDIA_ROOT, 'charts', filename3) save_figure(fig1, file_path1) save_figure(fig2, file_path2) save_figure(fig3, file_path3) context = { "data":data, "file_path1":file_path1, "file_path2":file_path2, "file_path3":file_path3, } pdf = render_to_pdf('database/chartdash.html', context) response = HttpResponse(pdf, content_type='application/pdf') filename = "reports_%s.pdf" %("12341231") content = "attachment; filename=%s" %(filename) response['Content-Disposition'] = content return response def downloadpdf(request): template = get_template('database/testdashdownload.html') context = { 'image_url': request.build_absolute_uri('/static/image.jpg') } # html = template.render(context) # pdf = HTML(string=html).write_pdf() response = HttpResponse(pdf, content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="person_list_pdf.pdf"' return response
Editor is loading...