Python 使用Flask和Google Colab呈现html模板时出错
我试图在GoogleColab上使用Flask呈现一个html模板,但尽管它看起来很简单,但我无法做到这一点。以下是我正在做的:Python 使用Flask和Google Colab呈现html模板时出错,python,html,flask,ngrok,Python,Html,Flask,Ngrok,我试图在GoogleColab上使用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_wi
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_模板需要相对路径,并在子文件夹模板中搜索
app = Flask(__name__, template_folder='/')
及
如果要使用绝对路径,则必须设置根文件夹-/-和带模板的文件夹
app = Flask(__name__, template_folder='/')
通常,render_模板需要相对路径,并在子文件夹模板中搜索它
app = Flask(__name__, template_folder='/')
及
如果要使用绝对路径,则必须设置根文件夹-/-和带模板的文件夹
app = Flask(__name__, template_folder='/')
在这里可以看到/content/gdrive/MyDrive/test/test.html是模板目录中html的路径。它尝试在templates目录中查找文件
说:
使用给定上下文呈现模板文件夹中的模板
默认模板文件夹是当前目录中的模板。我怎么知道?
它在里面
在这里可以看到/content/gdrive/MyDrive/test/test.html是模板目录中html的路径。它尝试在templates目录中查找文件
说:
使用给定上下文呈现模板文件夹中的模板
默认模板文件夹是当前目录中的模板。我怎么知道?
它在里面
首先检查是否有/content/gdrive/MyDrive/test/test.htmlI不确定render_模板是否使用绝对路径。通常它与相对路径(ie test.html)一起使用,并在子文件夹templates(ie.templates/test.html)中搜索它。如果文件在那里,好的,我将尝试使用相对部分,谢谢,它仍然是您在与app.py相同的文件夹中创建模板的相同ID?首先检查您是否有/content/gdrive/MyDrive/test/test.htmlI我不确定render_模板是否使用绝对路径。通常它与相对路径(ie test.html)一起使用,并在子文件夹templates(ie.templates/test.html)中搜索它。如果文件在那里,好的,我将尝试使用相对部分,谢谢,它仍然是与app.py在同一文件夹中创建模板相同的ID? class flask.Flask(..., template_folder='templates', ...)