Flask is a simple Python web framework. I think Sinatra is close to that in Ruby.
Flask uses a template engine called jinja2, which makes it feel like writing HTML in plain text. I'm used to HAML, so I tried using hamlish-jinja, which allows you to write HAML-like view templates in Flask.
$ pip install Flask
$ pip install Hamlish-Jinja
.
├── app.py
└── templates
└── index.haml
app.py
from flask import Flask, render_template
from werkzeug import ImmutableDict
class FlaskWithHamlish(Flask):
jinja_options = ImmutableDict(
extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_', 'hamlish_jinja.HamlishExtension']
)
app = FlaskWithHamlish(__name__)
app.jinja_env.hamlish_mode = 'indented'
app.jinja_env.hamlish_enable_div_shortcut = True
@app.route('/')
def index():
return render_template('index.haml')
if __name__ == '__main__':
app.run()
index.haml
%html
%head
%meta charset="utf-8"
%title
Hello World!
%body
%h1
Hello World!
$ python app.py
Now access localhost: 5000
on your web browser. You should see Hello World!
.
Flask seems to put templates in a directory named templates
by default.
As the name implies, Hamlish is "HAML-like", not HAML itself. This article was helpful to know the difference.
Even in the above example, in HAML it should be % meta {charset:" utf-8 "}
, but in Hamlish it is % meta charset =" utf-8 "
.
app.jinja_env.hamlish_mode = 'indented'
app.jinja_env.hamlish_enable_div_shortcut = True
An optional specification for Hamlish. Please see hamlish-jinja head family to see what kind of options are available.
I thought Flask was very compact and good. In addition, Hamlish seems to make your website fun and crisp.
Try using the web application framework Flask I learned a lot. Just by reading this, it seems that you can start building a small website immediately.
I always forget it. To have view file changes take effect without a server restart
$ FLASK_DEBUG=1 python app.py
And.