Introduction
Flask is a popular lightweight Python web MVC framework. Compared to Django
, Flask based projects are usually simpler to implement and easier to maintain. Flask is BSD licensed and developed by Pocono team.
This tutorial shows how to use Flask and how to integrate a Markdown parser/render into Flask.
Implementation
Flask based project has no file structure requirements. Python programs, view templates and static files such as javascript files, stylesheets and images can be organized freely. Here are the steps to implement it.
Create a project folder
mkdir pyblog
Install Prerequisites
pip install Flask
pip install Jinja2
pip install Flask-Misaka
Initiate the blogging program
Create a python file namely blog.py
. The code below gives an example to use Flask and view templates. index.html
is a Jinja2
template. The full code is shown in the appendix.
1 2 3 4 5 6 7 8 9 10 11 |
|
Run this project
Running the project is straightforward like executing a normal python script.
python blog.py
Integrate Markdown Renderer with Blogging Platform
Flask-Misaka is a project integrate Misaka with Flask. Misaka is a python binding for Sundown, which is a Markdown processing library in C.
The code below shows how Flask-Misaka is integrated into Flask. After app
is initialized by Flask, wrap app
by Misaka: Misaka(app)
.
1 2 3 4 |
|
After app
is initialized, markdown
can be used in jinja2
templates. The code below shows text
is rendered by markdown
.
1
|
|
The markdown content is written in readme.md
, which is rendered into a web page. The code below opens the file and stores the text into content
, which is rendered in the template.
1 2 3 4 5 6 |
|
Conclusion
This tutorial introduces Flask and Flask-Misaka, which integrates Markdown into web framework. The code shown in the appendix is not supposed for production use. It focuses on basic examples of Flask and markdown integration.
Appendix
blog.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
views/index.html
1 2 3 4 5 6 7 8 9 |
|
readme.md
1 2 3 |
|