简体   繁体   中英

Image not showing using Python and Flask on Google Colab

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM