Browse Source

latex pdf-exort

master
Walter Hupfeld 2 years ago
parent
commit
6fa510ae7f
  1. 1
      Dockerfile
  2. 10
      README.md
  3. 3
      backend/models.py
  4. 6
      co2budget/settings.py
  5. 187
      frontend/templates/frontend/index.html
  6. 191
      frontend/templates/frontend/view.html
  7. 69
      frontend/templates/output.tex
  8. 4
      frontend/urls.py
  9. 85
      frontend/views.py
  10. 2
      requirements.txt
  11. 2
      templates/frontend.html

1
Dockerfile

@ -3,6 +3,7 @@ FROM python:3.6
# update packagesdock
RUN apt-get -qq update
RUN apt-get install --yes apache2 apache2-dev
RUN apt-get install --yes tex-live
RUN pip install mod_wsgi
RUN mkdir /code

10
README.md

@ -19,3 +19,13 @@ Berechnung des CO2-Pfads einer Kommune
* https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Authentication
## LateX
* https://pypi.org/project/django-tex/
* https://texample.net/tikz/examples/tag/plots/
* https://www.namsu.de/Extra/pakete/Pie_Chart.html
* https://en.wikibooks.org/wiki/LaTeX/PGF/TikZ#Color Farbdefinitionen
* https://latexkurs.github.io/lecture/06_diagramme.pdf
* https://www.namsu.de/Extra/pakete/Pie_Chart.html

3
backend/models.py

@ -58,7 +58,7 @@ class Factsheet(models.Model):
class Calc:
def __init__(self, gemeindeId='03152012', targetyear=2030):
def __init__(self, gemeindeId, targetyear):
self.gemeindeId=gemeindeId
self.zieljahr=targetyear
@ -138,6 +138,7 @@ class Calc:
context = {
'populationDistrict' : self.round(populationDistrict),
'nameDistrict' : nameDistrict,
'gemeindeId' : self.gemeindeId,
'zieljahr' : self.zieljahr,
'kalkulationszeitraum' : kalkulationszeitraum,

6
co2budget/settings.py

@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_tex',
'backend',
'frontend'
]
@ -67,6 +68,11 @@ TEMPLATES = [
],
},
},
{
'NAME': 'tex',
'BACKEND': 'django_tex.engine.TeXEngine',
'APP_DIRS': True,
},
]
WSGI_APPLICATION = 'co2budget.wsgi.application'

187
frontend/templates/frontend/index.html

@ -1,24 +1,19 @@
<!-- population/templates/population/output.html -->
{% extends 'frontend.html' %}
{% block content %}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<style>
td {text-align:right;}
.input {background-color:aliceblue; padding: 0.5em; margin:1em 0 1em 0;}
input[type="number"] { min-width: 50px;}
#choice {width:20em;float:left;margin-left:1em;margin-right:2em;}
#filter {width:20em;margin-left:1em;margin-right:2em;}
.formular {background-color:beige; border: rgb(83, 68, 68) 1px solid; width:40em; height:12em;}
.formular {
background-color:beige;
border: rgb(83, 68, 68) 1px solid;
width:40em;
height:12em;
margin-top:3em;}
</style>
<script>
@ -44,177 +39,21 @@
<h1>CO2-Budget ihrer Kommune</h1>
Bitte geben Sie den Namen Ihrer Kommune ein und wählen
Ihre Kommune aus der Liste aus. Zusätzlich geben Sie das Jahr
an, an dem Ihre Kommune Klimaneutralität erreichen möchte.
<div class="formular">
<form method="get" action="/" autocomplete="off">
<form method="get" action="/view" autocomplete="off">
{% csrf_token %}
<input type="text" id="filter" placeholder="Stadt eingeben ..." autocomplete="off">
<select id="choice" name="choice" size=6></select>
<input type="number" name="targetyear" min='2025' max='2050' value="2030" placeholder="Zieljahr" >
<input type="number" name="targetyear" min='2025' max='2050' placeholder="Zieljahr" >
<input class='button btn-primary' type="submit" value="Abschicken">
</form>
</div>
<br style="clear:both;">
<h1>Daten für {{ nameDistrict }}</h1>
<div class="row">
<div class="col-md-6">
<h2>Angaben zur Kommune</h2>
<table class="table table-striped table-sm">
<tr><th>Name</th><td>{{ nameDistrict }}</sub></td></tr>
<tr><th>Einwohnerzahl</th><td>{{ populationDistrict }}</sub></td></tr>
</table>
<h2>Die wichtigen Zahlen</h2>
<table class="table table-striped">
<tr><th>Zieljahr der Klimaneutralität</th><td>{{ zieljahr }}</sub></td></tr>
<tr><th>Budget Kommune 2016</th><td>{{ budgetKommune2016 }} t</td></tr>
<tr><th>Verbleibendes Budget nach Zieljahr</th><td>{{ verbleibendesBudget }} t</td></tr>
<tr><th>THG-Bilanz 2018</th><td>{{ bilanzGES }} t</td></tr>
<tr><th>Gesamtkosten</th><td>{{ kostenGES }} €</td></tr>
<tr><th>Anteil der Kommune</th><td>{{ kostenAntKommune }} €</td></tr>
<tr><th>Gesamtkosten pro Jahr bis Zieljahr</th><td>{{kostenProJahr}} €</td></tr>
<tr><th>Anteil der Kommune bis Zieljahr</th><td>{{ kostenAnteilKommuneJahr }} €</td></tr>
<tr><th>Arbeitsplätze gesamt</th><td>{{ arbeitsplGES }}</td></tr>
<tr><th>kommunale Arbeitsplätze</th><td>{{ arbeitsplAntKommune }}</td></tr>
</table>
</div>
<div class="col-md-6">
<h2>CO<sub>2</sub> Emission 2018</h2>
<figure class="highcharts-figure">
<div id="container1"></div>
Gesamtmenge {{bilanzGES}} t
</figure>
<h3>Reduktionspfad CO<sub>2</sub>2 Emissionen 2016-2036</h3>
<figure class="highcharts-figure">
<div id="container2"></div>
</figure>
</div>
</div>
<div class="row">
</div>
<script>
Highcharts.chart('container1', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'CO2-Emission 2018'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
accessibility: {
point: {
valueSuffix: '%'
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %'
}
}
},
series: [{
name: 'Anteile',
colorByPoint: true,
data: [{
name: 'Energiewirtschaft',
y: {{ bilanzEWdata }},
//sliced: true,
//selected: true
}, {
name: 'Industrie',
y: {{ bilanzINdata }}
}, {
name: 'Gebäude',
y: {{ bilanzGBdata }}
}, {
name: 'Verkehr',
y: {{ bilanzGBdata }}
}, {
name: 'Landwirtschaft',
y: {{ bilanzLWdata }}
}, {
name: 'Sonstige',
y: {{ bilanzSOdata }}
}, ]
}]
});
Highcharts.chart('container2', {
chart: {
type: 'spline'
},
title: {
text: 'Reduktionspfad der Kommune'
},
subtitle: {
text: '{{ nameDistrict }}'
},
xAxis: {
categories: [
{% for key,val in emissionKommuneData.items %}
'{{ key }}',
{% endfor %}
]
},
yAxis: {
title: {
text: 'Temperature'
},
labels: {
formatter: function () {
return this.value + '°';
}
}
},
tooltip: {
crosshairs: true,
shared: true
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1
}
}
},
series: [{
name: 'CO2-Budget',
marker: {
symbol: 'square'
},
data: [
{% for key,val in emissionKommuneData.items %}
{{ val }},
{% endfor %}
]
}]
});
</script>
{% endblock content %}

191
frontend/templates/frontend/view.html

@ -0,0 +1,191 @@
<!-- population/templates/population/output.html -->
{% extends 'frontend.html' %}
{% block content %}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<style>
td {text-align:right;}
</style>
<h1>CO2-Budget ihrer Kommune</h1>
<br style="clear:both;">
<h1>Daten für {{ nameDistrict }}</h1>
<div class="row">
<div class="col-md-6">
<h2>Angaben zur Kommune</h2>
<table class="table table-striped table-sm">
<tr><th>Name</th><td>{{ nameDistrict }}</sub></td></tr>
<tr><th>Einwohnerzahl</th><td>{{ populationDistrict }}</sub></td></tr>
</table>
<p><a class="btn btn-primary" href="/download?id={{gemeindeId}}&year={{zieljahr}}" target="_blank">
Download Klimaberechnung</a>
</p>
<h2>Die wichtigen Zahlen</h2>
<table class="table table-striped">
<tr><th>Zieljahr der Klimaneutralität</th><td>{{ zieljahr }}</sub></td></tr>
<tr><th>Budget Kommune 2016</th><td>{{ budgetKommune2016 }} t</td></tr>
<tr><th>Verbleibendes Budget nach Zieljahr</th><td>{{ verbleibendesBudget }} t</td></tr>
<tr><th>THG-Bilanz 2018</th><td>{{ bilanzGES }} t</td></tr>
<tr><th>Gesamtkosten</th><td>{{ kostenGES }} €</td></tr>
<tr><th>Anteil der Kommune</th><td>{{ kostenAntKommune }} €</td></tr>
<tr><th>Gesamtkosten pro Jahr bis Zieljahr</th><td>{{kostenProJahr}} €</td></tr>
<tr><th>Anteil der Kommune bis Zieljahr</th><td>{{ kostenAnteilKommuneJahr }} €</td></tr>
<tr><th>Arbeitsplätze gesamt</th><td>{{ arbeitsplGES }}</td></tr>
<tr><th>kommunale Arbeitsplätze</th><td>{{ arbeitsplAntKommune }}</td></tr>
</table>
<h2>Download</h2>
<p><a class="btn btn-primary" href="/download?id={{gemeindeId}}&year={{zieljahr}}" target="_blank">Klimaberechnung</a> (PDF)</p>
</div>
<div class="col-md-6">
<h2>CO<sub>2</sub> Emission 2018</h2>
<figure class="highcharts-figure">
<div id="container1"></div>
Gesamtmenge {{bilanzGES}} t
</figure>
<h3>Reduktionspfad CO<sub>2</sub>2 Emissionen 2016-2036</h3>
<figure class="highcharts-figure">
<div id="container2"></div>
</figure>
</div>
</div>
<div class="row">
</div>
<script>
Highcharts.chart('container1', {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title: {
text: 'CO2-Emission 2018'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
accessibility: {
point: {
valueSuffix: '%'
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %'
}
}
},
series: [{
name: 'Anteile',
colorByPoint: true,
data: [{
name: 'Energiewirtschaft',
y: {{ bilanzEWdata }},
//sliced: true,
//selected: true
}, {
name: 'Industrie',
y: {{ bilanzINdata }}
}, {
name: 'Gebäude',
y: {{ bilanzGBdata }}
}, {
name: 'Verkehr',
y: {{ bilanzGBdata }}
}, {
name: 'Landwirtschaft',
y: {{ bilanzLWdata }}
}, {
name: 'Sonstige',
y: {{ bilanzSOdata }}
}, ]
}]
});
Highcharts.chart('container2', {
chart: {
type: 'spline'
},
title: {
text: 'Reduktionspfad der Kommune'
},
subtitle: {
text: '{{ nameDistrict }}'
},
xAxis: {
categories: [
{% for key,val in emissionKommuneData.items %}
'{{ key }}',
{% endfor %}
]
},
yAxis: {
title: {
text: 'Temperature'
},
labels: {
formatter: function () {
return this.value + '°';
}
}
},
tooltip: {
crosshairs: true,
shared: true
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1
}
}
},
series: [{
name: 'CO2-Budget',
marker: {
symbol: 'square'
},
data: [
{% for key,val in emissionKommuneData.items %}
{{ val }},
{% endfor %}
]
}]
});
</script>
{% endblock content %}

69
frontend/templates/output.tex

@ -0,0 +1,69 @@
% Author: Henri Menke
\documentclass[tikz,border=10pt]{article}
\usepackage{pgfplots}
\usepackage{pgf-pie}
\begin{document}
\section*{Daten für {{ nameDistrict }} }
\subsection*{Angaben zur Kommune}
\begin{tabular}{lr}
\hline
Name & {{ nameDistrict }} \\
\hline
Einwohnerzahl & {{ populationDistrict }} \\
\hline
\end{tabular}
\subsection*{Die wichtigen Zahlen}
\begin{tabular}{lr}
\hline
Zieljahr der Klimaneutralität & {{ zieljahr }} \\
Budget Kommune 2016 & {{ budgetKommune2016 }} t \\
Verbleibendes Budget nach Zieljahrb & {{ verbleibendesBudget }} t \\
THG-Bilanz 2018 & {{ bilanzGES }} t \\
Gesamtkosten & {{ kostenGES }}\\
Anteil der Kommune & {{ kostenAntKommune }}\\
Gesamtkosten pro Jahr bis Zieljahr & {{kostenProJahr}}\\
Anteil der Kommune bis Zieljahr & {{ kostenAnteilKommuneJahr }}\\
Arbeitsplätze gesamt & {{ arbeitsplGES }} \\
kommunale Arbeitsplätze & {{ arbeitsplAntKommune }} \\
\hline
\end{tabular}
\subsection*{Reduktionspfad von {{ nameDistrict }}}
\begin{tikzpicture}[scale=1.4, font=\sffamily]
\begin{axis}[
xlabel=Jahr,
ylabel=Emission in Mio. t CO2,
/pgf/number format/.cd,
use comma,
set thousands separator={}
]
\addplot+[smooth,color=red]coordinates { {{ verlauf }} };
\end{axis}
\end{tikzpicture}
\newpage
\subsection*{Verteilung der CO2-Emission auf die Sektoren}
Insgesamt verteilt sich die CO2-Emission von {{ nameDistrict }} von {{ bilanzGES }} auf die folgenden Sektoren:
\begin{tikzpicture}[y=1.2cm, x=1.2cm,font=\sffamily]
\pie[pos={8,0},
color={lime, orange, yellow, lightgray, teal, white}]
{{ verteilung }}
\end{tikzpicture}
\end{document}

4
frontend/urls.py

@ -1,10 +1,14 @@
from django.urls import path
from .views import index
from .views import view
from .views import search
from .views import download
urlpatterns = [
path('', index, name='frontend_index'),
path('view', view, name='view'),
path('search',search,name='search'),
path('download',download)
]

85
frontend/views.py

@ -6,29 +6,14 @@ from backend.models import County
from django.db.models import Q
from django.http import JsonResponse
from django_tex.shortcuts import render_to_pdf
from django.http import HttpResponseNotFound
# Create your views here.
def index(request):
template="frontend/index.html"
try:
targetyear = int(request.GET.get('targetyear'))
#county = request.GET.get('county')
gemeindeId = request.GET.get('choice')
print(gemeindeId)
#county_list = County.objects.filter(
# Q(Name__icontains=county) | Q(gemeindeId__icontains=county))
#gemeindeId=county_list[0].gemeindeId
if targetyear>2021 and targetyear<2050:
c=Calc(gemeindeId,targetyear)
return render(request, template, c.calculate())
else:
c=Calc()
return render(request, template, c.calculate())
except:
c=Calc()
return render(request, template, c.calculate())
return render(request, template)
def search(request):
@ -44,4 +29,66 @@ def search(request):
return ""
def view(request):
template="frontend/view.html"
try:
targetyear = int(request.GET.get('targetyear'))
gemeindeId = request.GET.get('choice')
if targetyear>2021 and targetyear<2050:
c=Calc(gemeindeId,targetyear)
return render(request, template, c.calculate())
else:
return HttpResponseNotFound('<h1>Parameter falsch</h1>')
except:
return HttpResponseNotFound('<h1>Parameter falsch</h1>')
def download(request):
try:
targetyear = int(request.GET.get('year'))
gemeindeId = request.GET.get('id')
if targetyear>2021 and targetyear<2050:
template_name = 'output.tex'
# Berechnung abrufen
c=Calc(gemeindeId,targetyear)
context=c.calculate()
# Da django-tex keine Templatesprache unterstützt, werden
# die notwendigen Parameter als String in der View generiert
# und dem Context hinzugefügt und an das TeX-Template übergeben
# Verlauf für den Plot
# Erzeuge kommaseparierte Liste mit den Daten
verlauf = context['emissionKommuneData']
strData=""
for key in verlauf:
strData += "("+str(key)+","+str(round(verlauf[key]/1000000,2))+")"
context['verlauf']=strData
print(strData)
# Verteilung für das Kreisdiagramm
# Erzeuge String mit der Form {80/Ja , 20/Nein}
# Angaben in Prozent
ew = str(round(context['bilanzEWdata']/context['bilanzGESdata']*100))
ind = str(round(context['bilanzINdata']/context['bilanzGESdata']*100))
gb = str(round(context['bilanzGBdata']/context['bilanzGESdata']*100))
vk = str(round(context['bilanzVKdata']/context['bilanzGESdata']*100))
lw = str(round(context['bilanzLWdata']/context['bilanzGESdata']*100))
so = str(round(context['bilanzSOdata']/context['bilanzGESdata']*100))
verteilung = "{"+ew+"/Energiewirtschaft,"+ind+"/Industrie,"
verteilung += gb+"/Gebäude,"+vk+"/Verkehr,"+lw+"/Landwirtschaft,"+so+"/Sonstiges"+"}"
context['verteilung']=verteilung
return render_to_pdf(request, template_name, context, filename='klimapfad.pdf')
else:
return HttpResponseNotFound('<h1>Parameter falsch</h1>')
except:
return HttpResponseNotFound('<h1>Parameter falsch</h1>')

2
requirements.txt

@ -1,2 +1,2 @@
django
django_tex

2
templates/frontend.html

@ -26,7 +26,7 @@
<a class="navbar-brand" href="/">Home</a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Eingabeformular</a>
<a class="nav-link" target="_blank" href="/download">Download (PDF)</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/backend/">Login</a>

Loading…
Cancel
Save