Python 100 project #12: First Website using Flask

This is just a mock web server for starter. I use flask as a web framework for now.

This time, I just implemented the first page(index.html) and the front page for Google Trends application(to show the wordcloud on the web page) only. Actual application will be added in later issue.

Output Example:

 

Here is the code:

webtest
|-facts.py ... store predefined dictionaries
|-app
  |-func.py ... functions to generate actual backend data
  |-forms.py ... classes to define forms
  |-routes.py ... routing for all directories. serving display
  |-templates
    |-base.html ... header and footer
    |-index.html
    |-gtrends_wc.html ... google trends application page
from flask_wtf import FlaskForm

from wtforms import SelectField, SubmitField
from wtforms.validators import DataRequired

from facts import COUNTRYDICT


class CountrySelectForm(FlaskForm):
    countries = [(c[1], c[0]) for c in COUNTRYDICT.items()]
    country = SelectField(label='country', choices=countries, validators=[DataRequired()])
    submit = SubmitField('Generate')
from flask import render_template

from app import app, func
from app.forms import CountrySelectForm
from app.func import *

@app.route('/')
@app.route('/index')
def index():
    msg = 'Hello World!'

    return render_template('index.html', title='Home', msg=msg)

@app.route('/gtrends_wordcloud', methods=['GET'])
def gen_gtrends_wordcloud():
    form = CountrySelectForm()

    return render_template('gtrends_wc.html', title='Google Trends Wordcloud', form=form)

<html>
    <head>
      {% if title %}
      <title>{{ title }} - Webtest</title>
      {% else %}
      <title>Welcome to Webtest</title>
      {% endif %}
    </head>
    <body>
        {% block content %}{% endblock %}
    </body>
</html>
{% extends "base.html" %}

{% block content %}
    <h1>Hello this is just a test website!</h1>
    <div>
        <p>{{ msg }}</p>
    </div>
{% endblock %}
{% extends "base.html" %}

{% block content %}
    <h1>Google Trends Wordcloud generator</h1>
    <form action="" method="post">
        <p>
            {{ form.country.label }} {{ form.country() }}<br>
            {% for error in form.country.errors %}
            <span style="color: red;">[{{ error }}]</span>
            {% endfor %}
        </p>
        <p>{{ form.submit() }}</p>
    </form>
{% endblock %}