使用 Flask 和 Google Colab 渲染 html 模板时出错
Error rendering html template with Flask and Google Colab
我正在尝试在 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 -
我知道某处一定有一个简单的错误,但由于我对这些东西不熟悉,所以我无法弄清楚。谢谢
通常 render_template
需要相对路径并在子文件夹 templates
中搜索它
app.py # file with Flask code
templates/test.html
和
return render_template('test.html')
如果你想使用绝对路径,那么你必须设置 root
文件夹 - /
- 和带有模板的文件夹
app = Flask(__name__, template_folder='/')
jinja2.exceptions.TemplateNotFound: /content/gdrive/MyDrive/test/test.html
在这里发现 flask 认为 /content/gdrive/MyDrive/test/test.html
是来自模板目录的 html 的路径。它会尝试在模板目录中查找文件。
render_template documentation 说:
Renders a template from the template folder with the given context.
默认模板文件夹是当前目录中的templates
。我怎么知道?
它在 flask documentation.
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')
我正在尝试在 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 -
我知道某处一定有一个简单的错误,但由于我对这些东西不熟悉,所以我无法弄清楚。谢谢
通常 render_template
需要相对路径并在子文件夹 templates
app.py # file with Flask code
templates/test.html
和
return render_template('test.html')
如果你想使用绝对路径,那么你必须设置 root
文件夹 - /
- 和带有模板的文件夹
app = Flask(__name__, template_folder='/')
jinja2.exceptions.TemplateNotFound: /content/gdrive/MyDrive/test/test.html
在这里发现 flask 认为 /content/gdrive/MyDrive/test/test.html
是来自模板目录的 html 的路径。它会尝试在模板目录中查找文件。
render_template documentation 说:
Renders a template from the template folder with the given context.
默认模板文件夹是当前目录中的templates
。我怎么知道?
它在 flask documentation.
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')