[PYTHON] Hello World with Flask + Hamlish

Overview

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.

Preparation

$ pip install Flask
$ pip install Hamlish-Jinja

file organization

.
├── 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!

Run

$ 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.

Difference from the original HAML

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 ".

Option specification

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.

Impressions

I thought Flask was very compact and good. In addition, Hamlish seems to make your website fun and crisp.

References

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.

Postscript

I always forget it. To have view file changes take effect without a server restart

$ FLASK_DEBUG=1 python app.py

And.

Recommended Posts

Hello World with Flask + Hamlish
Hello world with flask
hello world with ctypes
Hello, World with Docker
Draw hello world with mod_wsgi
Until hello world with zappa
Python starting with Hello world!
Hello world
Hello, world! With virtual CAN communication
[Note] Hello world output with python
Hello World in Flask [Appropriate memo]
Hello World! By QPython with Braincrash
Until Hello World with Flask + uWSGI + Nginx @ Sakura's VPS (CentOS 6.6)
Hello World and face detection with opencv-python 4.2
Hello World with Raspberry Pi + Minecraft Pi Edition
Pymacs hello world
Flask tutorial (from installation to hello world)
cython hello world
Hello World! By QPython with Brainfu * k
Re: Heroku life begin with Flask from zero - Environment and Hello world -
Hello World and face detection with OpenCV 4.3 + Python
Hello world with full features of Go language
Say hello to the world with Python with IntelliJ
Hello World with nginx + uwsgi + python on EC2
Flask Hello World cannot be displayed on VPS
Create a one-file hello world application with django
First python ① Environment construction with pythonbrew & Hello World !!
Create a "Hello World" (HTTP) server with Tornado
web2py memo: Hello World
RabbitMQ Tutorial 1 ("Hello World!")
IP restrictions with Flask
Hello World on Django
Django's first Hello World
Programming with Python Flask
Predicting Kaggle's Hello World, Titanic Survivors with Logistic Regression-Modeling-
Hello World with Google App Engine (Java 8) + Spring Boot + Gradle
Hello World with Google App Engine (Java 8) + Servlet API 3.1 + Gradle
Deploy Flask with ZEIT Now
Touch Flask + run with Heroku
Unit test flask with pytest
API with Flask + uWSGI + Nginx
SNS Flask (Ajax) made with Flask
Web application development with Flask
Programming language in "Hello World"
Hello World in GO language
View flask coverage with pytest-cov
Web application with Python + Flask ② ③
Predicting Kaggle's Hello World, Titanic Survivors with Logistic Regression-Prediction / Evaluation-
File upload with Flask + jQuery
Hello World (beginners) on Django
Hello World with Google App Engine (Java 11) + Spring Boot + Gradle
Web application with Python + Flask ④
Start with Windows, not so scary Nim ① It's like hello world.
Python beginners tried Hello World in 30 seconds using the micro-framework Flask
SNS Flask (Model) edition made with Flask
[LINE login] Verify state with Flask
Let's do "Hello World" in 40 languages! !!
SNS Python basics made with Flask
[Memo] Links for developing with Flask
Introduction to TensorFlow --Hello World Edition
Hello world! (Minimum Viable Block Chain)