Insert Images Using Flask-FlastPages

I created my blog using Flask-FlatPages and the posts are in Markdown, the problem I am facing is inserting images into my blog post. The traditional way of inserting images in markdowns does not work.

I also tried this without success:

![image]({{ url_for('static', filename="img/my_image.jpg") }})
0
source share
3 answers

Here is a quick fix! let's say we have an image folder and we want to useimages/flower.jpg

Step 1 . Put the folder imagesin the folder static.

Step 2 . In the text, connect the image with ../static/images/flower.jpg.

eg:

![flower](../static/images/flower.jpg)

or

<img src="../static/images/flower.jpg" alt="flower">
+2
source

Here is what worked for me. Turns out FLATPAGES_HTML_RENDERER is needed to achieve this.

Here is the code:

def my_renderer(text):
    prerendered_body = render_template_string(text)
    return pygmented_markdown(prerendered_body)

app = Flask(__name__)
app.config['FLATPAGES_HTML_RENDERER'] = my_renderer
pages = FlatPages(app)

: https://github.com/SimonSapin/Flask-FlatPages/pull/1

+1

html.

I think this is because the bleach.clean () function.

You can try adding to allowed_tags.

allowed_tags = ['img']
allowed_attrs = {'img':['src','alt']};
target.body_html = bleach.linkify(bleach.clean(markdown(value,output_format='html'),tags=allowed_tags,attributes=allowed_attrs,strip=True));
-1
source

All Articles