Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Flask: Jinja doesn't support absolute file paths #10

Open
jyn514 opened this issue Apr 20, 2018 · 2 comments
Open

Flask: Jinja doesn't support absolute file paths #10

jyn514 opened this issue Apr 20, 2018 · 2 comments

Comments

@jyn514
Copy link
Owner

jyn514 commented Apr 20, 2018

Docs

I was trying to run {% include <filepath> %} in templates/index.html. Unfortunately, jinja gives an error that the file doesn't exist even when it's there because it doesn't support arbitrary file paths. My current workaround is to put ln -s / root in templates and {% include 'root/' + <filepath> %}, which is hardly ideal.

The easiest way to fix this is just have snaps in the working directory, but I like the idea of having a global cache. Still considering how to implement this.

Traceback

Traceback (most recent call last):
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/joshua/Documents/Programming/Python/Colahacks/index.py", line 39, in main
    return render_template("index.html", **session)
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/templating.py", line 134, in render_template
    context, ctx.app)
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/templating.py", line 116, in _render
    rv = template.render(context)
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/home/joshua/Documents/Programming/Python/Colahacks/templates/index.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "/home/joshua/Documents/Programming/Python/Colahacks/templates/base.html", line 17, in top-level template code
    {% block content %}No Content{% endblock %}
  File "/home/joshua/Documents/Programming/Python/Colahacks/templates/index.html", line 80, in block "content"
    <hr>{% include SAVE_ROOT + '/' + project + '/' + current + '/output.html' %}
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/templating.py", line 57, in get_source
    return self._get_source_fast(environment, template)
  File "/usr/local/lib/miniconda/lib/python3.6/site-packages/flask/templating.py", line 85, in _get_source_fast
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: /home/joshua/.cache/snap/Colahacks/08b0789c0db460d567d0fdb999a71d0a97f49da6/output.html
@jyn514 jyn514 changed the title git: Jinja doesn't support absolute file paths Flask: Jinja doesn't support absolute file paths Apr 20, 2018
@hdamron17
Copy link
Collaborator

My fix was to just pass the file texts as a string. No code file should be so long that it’s a huge issue at least for now. (Since our purpose is instruction)

@jyn514
Copy link
Owner Author

jyn514 commented May 19, 2018

This doesn't work for any sort of binary file. I know this is primarily for instruction, but it will crash on pictures, databases, anything that isn't valid unicode.

Traceback:

127.0.0.1 - - [19/May/2018 16:19:11] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [19/May/2018 16:19:13] "GET /?project=Colahacks HTTP/1.1" 200 -
127.0.0.1 - - [19/May/2018 16:19:14] "GET /?codefile=.backupzips%2Fcppdemo1.zip HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/joshua/.local/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/joshua/.local/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/home/joshua/.local/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/joshua/.local/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/joshua/.local/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/joshua/.local/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/joshua/.local/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/joshua/.local/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/joshua/.local/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/joshua/.local/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/joshua/Documents/Programming/Python/Colahacks/index.py", line 154, in main
    other_vars['code_file'] = cfile.read()
  File "/usr/lib64/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 11: invalid start byte

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants