1

I'm trying to render an html template using Flask on Google Colab, but although it looks very simple i am unable to do so. Here's what i'm doing:

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

My html file looks like this (its just a test file)

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

</body>
</html>

and i get an internal server error. Here's the full output of the error:

 * 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 -

I know that there must be a simple error going on somewhere, but since i'm new to these kind of stuff, i cannot figure it out. Thanks

Olive Yew
  • 351
  • 4
  • 13

2 Answers2

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

Catch here that flask thinks /content/gdrive/MyDrive/test/test.html is the path of html from templates directory. It tries to find file inside templates directory.

render_template documentation says:
Renders a template from the template folder with the given context.

Default template folder is templates in currently directory. How do I know?
It's in flask documentation.

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

Which leads us to solution of changing template_folder.

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

@app.route('/')
def home():
  return render_template('test.html')
nkit
  • 84
  • 6
  • Thank you so much Nkit, this below has been very helpfull for me: "app = Flask(__name__, template_folder='/content/gdrive/MyDrive/test')" – Josh Bagwel Jul 09 '21 at 13:09
1

Normally render_template expects relative path and it search it in subfolder templates

app.py  #  file with Flask code
templates/test.html

and

return render_template('test.html')

If you want to use absolute path then you have to set root folder - / - and folder with templates

app = Flask(__name__, template_folder='/')
furas
  • 134,197
  • 12
  • 106
  • 148