Commit cfc3102f authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Polish controller graphs and its views.

parent b32b1d76
""" Controllers for building graphs using pandas library
"""
import base64
import io
from citations_tools import (PLOT_ALL,
PLOT_PER_DOMAIN,
PLOT_PER_PROJECT,
......@@ -21,7 +17,14 @@ from models.selector import YEAR_SUBMIT
def citations():
"""Return graphs showing citations
"""Generate figures with citations plots.
Returns:
dict:
* fig1 (matplotlib.Figure)
* fig2 (matplotlib.Figure)
* fig3 (matplotlib.Figure)
* fig4 (matplotlib.Figure)
"""
import pandas as pd
......@@ -142,25 +145,7 @@ def citations():
#
# rendering
#
dct = {}
buf1 = io.BytesIO()
fig1.savefig(buf1, format="svg")
dct["fig1"] = base64.b64encode(buf1.getbuffer()).decode("ascii")
buf2 = io.BytesIO()
fig2.savefig(buf2, format="svg")
dct["fig2"] = base64.b64encode(buf2.getbuffer()).decode("ascii")
buf3 = io.BytesIO()
fig3.savefig(buf3, format="svg")
dct["fig3"] = base64.b64encode(buf3.getbuffer()).decode("ascii")
buf4 = io.BytesIO()
fig4.savefig(buf4, format="svg")
dct["fig4"] = base64.b64encode(buf4.getbuffer()).decode("ascii")
return dict(**dct)
return dict(fig1=fig1, fig2=fig2, fig3=fig3, fig4=fig4)
def dashboard():
......@@ -168,6 +153,10 @@ def dashboard():
Cumulative distribution for the publications are shown for
the current year.
Returns:
dict:
* fig (matplotlib.Figure)
"""
current_year = request.now.year
dashboard_start = current.app.dashboard_start_year
......@@ -219,12 +208,8 @@ def dashboard():
linechart(db, selector, target=ax2, title=title)
# delegate the rendering to the view
buf = io.BytesIO()
fig.savefig(buf, format="svg")
data = base64.b64encode(buf.getbuffer()).decode("ascii")
response.view = "graphs/index.html"
return dict(data=data)
return dict(fig=fig)
def publications_versus_time():
......@@ -249,12 +234,5 @@ def publications_versus_time():
linechart(db, selector, target=ax)
# delegate the rendering to the view
extension = request.extension
fmt = ("svg" if extension == "html" else extension)
buf = io.BytesIO()
fig.savefig(buf, format=fmt)
data = base64.b64encode(buf.getbuffer()).decode("ascii")
response.view = "graphs/index.%s" % extension
return dict(data=data)
response.view = f"graphs/index.{request.extension}"
return dict(fig=fig)
{{
#--------------------------------------------------------------------------
#
# The python controller return image as a string:
# - data (string)
# The python controller return matplotlib image(s)
#
#--------------------------------------------------------------------------
import base64
import io
import urllib.parse
# encode special character bo be used in the image URI
fig1 = urllib.parse.quote(fig1)
fig2 = urllib.parse.quote(fig2)
fig3 = urllib.parse.quote(fig3)
fig4 = urllib.parse.quote(fig4)
buf1 = io.BytesIO()
fig1.savefig(buf1, format="svg")
data1 = base64.b64encode(buf1.getbuffer()).decode("ascii")
data1 = urllib.parse.quote(data1)
buf2 = io.BytesIO()
fig2.savefig(buf2, format="svg")
data2 = base64.b64encode(buf2.getbuffer()).decode("ascii")
data2 = urllib.parse.quote(data2)
buf3 = io.BytesIO()
fig3.savefig(buf3, format="svg")
data3 = base64.b64encode(buf3.getbuffer()).decode("ascii")
data3 = urllib.parse.quote(data3)
buf4 = io.BytesIO()
fig4.savefig(buf4, format="svg")
data4 = base64.b64encode(buf4.getbuffer()).decode("ascii")
data4 = urllib.parse.quote(data4)
}}
......@@ -19,7 +34,7 @@
1 General
</h2>
<img src="data:image/svg+xml;base64,{{=fig1}}" height=400>
<img src="data:image/svg+xml;base64,{{=data1}}" height=400>
<h2 class="dbui-h2" style="margin-bottom: 0ex; font-variant: small-caps;">
2 Evolution of estimators as function of time
......@@ -28,19 +43,19 @@
2.1 Total number of citations
</h2>
<img src="data:image/svg+xml;base64,{{=fig2}}" height=400>
<img src="data:image/svg+xml;base64,{{=data2}}" height=400>
<h2 class="dbui-h2" style="margin-bottom: 0ex;margin-top: 0ex;">
2.2 Average number of citations per article
</h2>
<img src="data:image/svg+xml;base64,{{=fig3}}" height=400>
<img src="data:image/svg+xml;base64,{{=data3}}" height=400>
<h2 class="dbui-h2" style="margin-bottom: 0ex;margin-top: 0ex;">
2.3 h-index
</h2>
<img src="data:image/svg+xml;base64,{{=fig4}}" height=400>
<img src="data:image/svg+xml;base64,{{=data4}}" height=400>
{{
from datetime import datetime
......
{{
#--------------------------------------------------------------------------
#
# The python controller return image as a string:
# - data (string)
# The python controller return matplotlib.Image
#
#--------------------------------------------------------------------------
import base64
import io
import urllib.parse
# encode special charactier bo be used in the image URI
buf = io.BytesIO()
fig.savefig(buf, format="svg")
data = base64.b64encode(buf.getbuffer()).decode("ascii")
data = urllib.parse.quote(data)
}}
......
{{
import base64
import io
buf = io.BytesIO()
fig.savefig(buf, format="pdf)
data = base64.b64encode(buf.getbuffer()).decode("ascii")
response.body = io.StringIO()
response.headers['Content-Type']='application/pdf'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment