Image not showing using Python and Flask on Google Colab

Viewed 547

I want to show an image using Python and Flask on Google Colab, however, the resulting webpage doesn't show the image. Please find below my code. Any suggestions would be helpful.

Regards, Ben

from google.colab import drive
drive.mount('/content/drive')

from flask_ngrok import run_with_ngrok
from flask import Flask, render_template

app = Flask(__name__, template_folder='/content/drive/MyDrive/ColabNotebooks/flasktest/templates', static_folder='/content/drive/MyDrive/ColabNotebooks/flasktest/static')
run_with_ngrok(app)

@app.route('/')
def main():
    return '<html><body><img src="cross.png"></body></html>'

if __name__ == "__main__":
    app.run()
1 Answers

You can try to use url_for

@app.route('/')
def main():
    return '<html><body><img src="{{url_for('static',filename = './cross.png')}}"></body></html>'

'static' there refers to your static_folder path.

Related