sqlite - Using markdown formatting on queried SQL in flask app -
in flask app, retrieving entries sqlite database, display on html page, code below:
@app.route('/') def index(): db = get_db() cur = db.execute('select title, text entries order id desc') entries = cur.fetchall() return render_template('index.html', entries=entries)
i use markdown format content. have markdown installed, , use on sql query used below on raw data.
import markdown flask import flask flask import render_template flask import markup app = flask(__name__) @app.route('/') def index(): content = """ chapter ======= section ------- * item 1 * item 2 """ content = markup(markdown.markdown(content)) return render_template('index.html', **locals()) app.run(debug=true)
which works yield chapter/section/item stuff marked down when pulled in html template. i not want install flask-markdown want regular markdown, if possible.
there 2 options:
you can render markdown before passing
render_template
:@app.route('/') def index(): db = get_db() cur = db.execute('select title, text entries order id desc') entries = [markup(markdown(entry) entry in cur.fetchall()] return render_template('index.html', entries=entries)
you can register template filter , use in template:
@app.template_filter("markdown") def render_markdown(markdown_text): return markup(markdown(markdown_text))
then, in template can call
markdown
filter:{% entry in entries %} <article> {{entry | markdown}} </article> {% endfor %}
Comments
Post a Comment