首页 > 解决方案 > 使用 Flask 和 Google Colab 渲染 html 模板时出错

问题描述

我正在尝试在 Google Colab 上使用 Flask 呈现一个 html 模板,但是虽然它看起来很简单,但我无法这样做。这就是我正在做的事情:

from flask_ngrok import run_with_ngrok
from flask import Flask, render_template , request 


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

app = Flask(__name__)

run_with_ngrok(app)
@app.route('/')
def home():
  return render_template('/content/gdrive/MyDrive/test/test.html')


if __name__ == '__main__':
   app.run()

我的 html 文件看起来像这样(它只是一个测试文件)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Talk to the bot</title>
</head>
<body>

</body>
</html>

我得到一个内部服务器错误。这是错误的完整输出:

 * Serving Flask app "__main__" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Running on http://3b510d3973eb.ngrok.io
 * Traffic stats available on http://127.0.0.1:4040
[2021-02-03 15:29:42,269] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "<ipython-input-32-84fc2b6eea6b>", line 7, in home
    return render_template('/content/gdrive/MyDrive/test/test.html')
  File "/usr/local/lib/python3.6/dist-packages/flask/templating.py", line 138, in render_template
    ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "/usr/local/lib/python3.6/dist-packages/jinja2/environment.py", line 930, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "/usr/local/lib/python3.6/dist-packages/jinja2/environment.py", line 883, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/usr/local/lib/python3.6/dist-packages/jinja2/environment.py", line 857, in _load_template
    template = self.loader.load(self, name, globals)
  File "/usr/local/lib/python3.6/dist-packages/jinja2/loaders.py", line 115, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/usr/local/lib/python3.6/dist-packages/flask/templating.py", line 60, in get_source
    return self._get_source_fast(environment, template)
  File "/usr/local/lib/python3.6/dist-packages/flask/templating.py", line 89, in _get_source_fast
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: /content/gdrive/MyDrive/test/test.html
127.0.0.1 - - [03/Feb/2021 15:29:42] "GET / HTTP/1.1" 500 -
127.0.0.1 - - [03/Feb/2021 15:29:42] "GET /favicon.ico HTTP/1.1" 404 -

我知道某处肯定发生了一个简单的错误,但是由于我对这类东西不熟悉,所以我无法弄清楚。谢谢

标签: pythonhtmlflaskngrok

解决方案


jinja2.exceptions.TemplateNotFound: /content/gdrive/MyDrive/test/test.html

抓住这里,flask 认为 /content/gdrive/MyDrive/test/test.html是模板目录中的 html 路径。它尝试在模板目录中查找文件。

render_template 文档说:
Renders a template from the template folder with the given context.

默认模板文件夹templates在当前目录中。我怎么知道?
它在烧瓶文档中

class flask.Flask(..., template_folder='templates' , ...)

这使我们找到了改变的解决方案template_folder

app = Flask(__name__, template_folder='/content/gdrive/MyDrive/test')

@app.route('/')
def home():
  return render_template('test.html')

推荐阅读