Ejemplo n.º 1
0
def generate_top_5(props):

    db = get_db()
    return_list = []

    units = db.units.find({})
    prop_units = dict()

    for unit in units:
        prop_units[unit["property"]] = unit["unit"]

    for prop in props:

        df = pd.DataFrame(
            db.country_data.find({}, {
                prop: 1,
                'Country': 1
            }).sort(prop, -1)[:5])

        # Defines the plot
        ax = df.plot.bar()
        ax.get_legend().remove()

        ax.set_xticklabels(df['Country'],
                           fontsize=15,
                           color='red',
                           fontfamily='sans-serif',
                           fontstyle='italic',
                           fontvariant='small-caps',
                           fontweight='heavy',
                           rotation=15,
                           ha='right')

        ax.set_title(prop,
                     fontsize=15,
                     fontweight='heavy',
                     fontvariant='normal',
                     fontfamily='sans-serif',
                     color='green')
        if prop in prop_units.keys():
            ax.set_ylabel(prop_units[prop])

        fig = ax.get_figure()

        # Convert plot to PNG image
        pngImage = io.BytesIO()
        FigureCanvas(fig).print_png(pngImage)

        # Encode PNG image to base64 string
        pngImageB64String = "data:image/png;base64,"
        pngImageB64String += base64.b64encode(
            pngImage.getvalue()).decode('utf8')
        return_list.append(pngImageB64String)

    return return_list
Ejemplo n.º 2
0
def scandinavia(params=['GDP']):
    '''Docstring'''

    db = get_db()
    properties = list(db.country_data.find_one({}).keys())[2:-1]

    if request.method == 'POST':
        plots = generate_scandinavian(request.form)
        return render_template("scandinavian/bar.html",
                               images=plots,
                               props=properties)
    else:
        return render_template("scandinavian/welcome.html", props=properties)
Ejemplo n.º 3
0
def scandinavia(params = ['GDP']):
    '''Docstring'''

    db = get_db()
    properties = list(db.country_data.find_one({}).keys())
    remove = ['Country', "_id", "iso"]
    for rem in remove:
        properties.remove(rem)
    properties.sort()

    if request.method == 'POST':
        plots = generate_scandinavian(request.form)
        return render_template("scandinavian/bar.html", images=plots, props=properties)
    else:
        return render_template("scandinavian/welcome.html", props=properties)
Ejemplo n.º 4
0
def generate_bottom_5(props):

    db = get_db()
    return_list = []
    for prop in props:

        df = pd.DataFrame(
            db.country_data.find({
                prop: {
                    "$ne": None
                }
            }, {
                prop: 1,
                'Country': 1
            }).sort(prop, 1)[:5])

        # Defines the plot
        ax = df.plot.bar(rot=0, figsize=(8, 6))

        ax.set_xticklabels(df['Country'],
                           fontsize=15,
                           color='red',
                           fontfamily='sans-serif',
                           fontstyle='italic',
                           fontvariant='small-caps',
                           fontweight='heavy',
                           rotation=15,
                           ha='right')
        ax.set_title('Bottom 5 Countries',
                     fontsize=20,
                     fontweight='heavy',
                     fontvariant='normal',
                     fontfamily='sans-serif',
                     color='green')

        fig = ax.get_figure()

        # Convert plot to PNG image
        pngImage = io.BytesIO()
        FigureCanvas(fig).print_png(pngImage)

        # Encode PNG image to base64 string
        pngImageB64String = "data:image/png;base64,"
        pngImageB64String += base64.b64encode(
            pngImage.getvalue()).decode('utf8')
        return_list.append(pngImageB64String)

    return return_list
Ejemplo n.º 5
0
def generate_scandinavian(props):
    '''Docstring'''

    db = get_db()
    countries = ['Norway', 'Sweden', 'Denmark', 'Finland', 'Iceland']
    return_list = []

    for prop in props:
        df = pd.DataFrame(
            db.country_data.find({'Country': {
                "$in": countries
            }}, {
                prop: 1,
                'Country': 1
            }))

        # Plot modifications
        ax = df.plot.bar(rot=0)
        ax.set_xticklabels(df['Country'],
                           fontsize=15,
                           color='red',
                           fontfamily='sans-serif',
                           fontstyle='italic',
                           fontvariant='small-caps',
                           fontweight='heavy',
                           rotation=15,
                           ha="right")
        ax.set_title('Scandinavian Countries',
                     fontsize=20,
                     fontweight='heavy',
                     fontvariant='normal',
                     fontfamily='sans-serif',
                     color='green')
        fig = ax.get_figure()

        # Converts plot --> PNG image
        pngImage = io.BytesIO()
        FigureCanvas(fig).print_png(pngImage)

        # Encodes PNG image --> base64 string
        pngImageB64String = "data:image/png;base64,"
        pngImageB64String += base64.b64encode(
            pngImage.getvalue()).decode('utf8')
        return_list.append(pngImageB64String)
    return return_list
Ejemplo n.º 6
0
def top5(params=['GDP']):

    # Each plot should be generated from standalone functions,  no plot generating code should be in this function in the end.
    db = get_db()

    properties = list(db.country_data.find_one({}).keys())
    remove = ['Country', "_id", "iso"]
    for rem in remove:
        properties.remove(rem)
    properties.sort()

    if request.method == 'POST':

        plots = generate_top_5(request.form)

        return render_template("top5/bar.html", images=plots, props=properties)
    else:
        return render_template("top5/welcome.html", props=properties)
Ejemplo n.º 7
0
def bottom5(params=['GDP']):

    db = get_db()

    properties = list(db.country_data.find_one({}).keys())
    remove = ['Country', "_id", "iso"]
    for rem in remove:
        properties.remove(rem)
    properties.sort()

    if request.method == 'POST':

        plots = generate_bottom_5(request.form)

        return render_template("bottom5/bar.html",
                               images=plots,
                               props=properties)
    else:
        return render_template("bottom5/welcome.html", props=properties)
Ejemplo n.º 8
0
def country_stat():
    # If there is a POST request to this url, we generate content to return

    if request.method == 'POST':
        data = request.get_json()
        data = data['cname']
        db = get_db()
        country = db.country_data.find_one({"iso": data})
        units = db.units.find({})
        prop_units = dict()

        for unit in units:
            prop_units[unit["property"]] = unit["unit"]

        return render_template('map/country.html',
                               data=country,
                               units=prop_units)
    # If no POST request, redirect to the map index
    return redirect(url_for('map.index'))
Ejemplo n.º 9
0
def generate_scandinavian(props):
    '''Docstring'''

    db = get_db()
    countries = ['Norway', 'Sweden', 'Denmark', 'Finland', 'Iceland', 'Faroe Islands']
    return_list = []
    units = db.units.find({})
    prop_units = dict()
    for unit in units:
        prop_units[unit["property"]] = unit["unit"]

    for prop in props:
        df = pd.DataFrame(db.country_data.find({'Country': {
            "$in": countries}}, {prop: 1, 'Country': 1}))

        # Plot modifications
        df = df.replace([None], 0)
        ax = df.plot.bar(rot=0)
        ax.get_legend().remove()
        ax.set_xticklabels(df['Country'],fontsize=15,color='red',
                            fontfamily='sans-serif',fontstyle='italic',
                            fontvariant='small-caps',fontweight='heavy', rotation=15, ha="right")
        ax.set_title(prop,
                            fontsize=15,fontweight='heavy',fontvariant='normal',
                            fontfamily='sans-serif',color='green')
        if prop in prop_units.keys():
            ax.set_ylabel(prop_units[prop])
        fig = ax.get_figure()

        # Converts plot --> PNG image
        pngImage = io.BytesIO()
        FigureCanvas(fig).print_png(pngImage)

        # Encodes PNG image --> base64 string
        pngImageB64String = "data:image/png;base64,"
        pngImageB64String += base64.b64encode(pngImage.getvalue()).decode('utf8')
        return_list.append(pngImageB64String)
    return return_list
Ejemplo n.º 10
0
def index():
    db = get_db()
    mapjson = dumps(list(db.countries.geo.find({})))
    return render_template('map/index.html', geojson=mapjson)