Forums

flask-based website stops rendering markdown after some minutes

Hi! I could need some advice with this code. I want this README.md-file rendered on my site. It works perfectly for some minutes, and then it starts only showing Image Alt Text instead. This is my first time trying flask. Do I need to do something special for it to be rendered permanently? Any advice is helpful

Best,

My flask_app.py python-file looks like this:

import markdown
from flask import Flask
import markdown.extensions.fenced_code

app = Flask(__name__)


@app.route("/")
def index():
    readme_file = open("/home/username/mysite/README.md", "r")
    md_template_string = markdown.markdown(
        readme_file.read(), extensions=["fenced_code"]
    )

    return md_template_string

And I use the standard wsgi.py-file to import it, with:

# This file contains the WSGI configuration required to serve up your
# web application at http://<your-username>.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# The below has been auto-generated for your Flask project

import sys

# add your project directory to the sys.path
project_home = '/home/username/mysite'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from flask_app import app as application  # noqa

Could you give example url, here or send it to support@pythonanywhere.com

Thanks! I provided a url in email.

In the meantime I have followed this beginners-guide, and tried to use the .html-version of the markdown-file instead: https://blog.pythonanywhere.com/121/ I guess I will find out the coming days if it stays up or not. The html-template has a structure like the one below.

I want to store figures on the website, each image is a .png-filetype with a size 15KB. Image-file-urls stays the same, but , images are overwritten after each daily task with their same filename.

  • Is using a html-template as shown below, the best way to do this?
  • How can I make sure the images on the website appear updated, as soon as the files are overwritten?

Best,

<html>

<figure>
<img src="image_url_1.png"
alt="Image of a plot showing development since 2023-07-16." />
</figure>

<figure>
...
</figure>

<figure>
...
</figure>

<figure>
...
</figure>

</html>

You can never be 100% sure about how client's browser is caching, but if the file name keeps changing (and you can easily template it in) that would not be cached.

Thanks. I am trying to understand.

Currently filename does not change, but the data in the png is changed every day. If the clients browser allows it, will the images referenced inside the html-template stay viewable for the full period the website is up?

Best,

To make sure that the images arent cached, append each image file with the day they were created. Then when you render the template, load todays images

Okay, will try to do this. Thanks for the feedback. I guess I can just point the img src to the new image filename, but how can I also do the following "re-rendering" of the template?

Best,

When you render the template (using the render_template function) then you can pass in extra parameters -- like the list of comments in the tutorial you linked to. So you could pass in the datestamp that you're appending to the image filenames there, and then in the template you'd append it to the URLs you use in the src attributes of your <img> tags.

Thanks alot! Will try this out asap. Best,