question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implementation of Jinja2 Variables

See original GitHub issue

First of all : Eel is amazing ! This is a project I’d love to participate with.

One of the features I find lacking is the implementation of Jinja Variables. While the documentation and the options allow for inserting and using templates (with the jinja_templates argument in the start() function), I did not see any possibility to transfer variables within the templates.

It would be nice if this could be included either as a decorator or in a more “flask-like” fashion in the return of a function.

One solution that I tried to fiddle with but to no avail, was to create a decorator for the _static function (in the __init__.py) that seems to handle Jinja’s template.render() function.

Here’s what I got so far:

def jinjaVariables(_static):
    def innerFunction(path, jinja_variable=None):
        # the decorator recieved a Jinja Variable
        if "jinja_variables" is not None:
            response = None
            if 'jinja_env' in _start_args and 'jinja_templates' in _start_args:
                template_prefix = _start_args['jinja_templates'] + '/'
                if path.startswith(template_prefix):
                    n = len(template_prefix)
                    template = _start_args['jinja_env'].get_template(path[n:])
                    response = btl.HTTPResponse(template.render(jinja_variable))

            if response is None:
                response = btl.static_file(path, root=root_path)

            _set_response_headers(response)
            return response

        _static(path)

    return innerFunction

The idea was to create a global dictionary variable called jinja_variable and to update that variable depending on the page you want to visit.

If you were to include the jinja_variable in the kwargs of the start() function, wouldn’t it be possible to dynamically change the output of the html page by accessing the required keys ?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

1reaction
eddyizmcommented, Jan 28, 2022

So am I correct in understanding that you can’t pass a python variable to jinja or for example a data table?

1reaction
forno96commented, Jul 6, 2021

There’s some update on this topic? I’m falling in love with this project and having the possibility to pass context to jinja would be super helpfull

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jinja2 Tutorial - Part 1 - Introduction and variable substitution |
Jinja expects this variable to be provided to the engine by you and it simply takes the value referred by the variable replacing...
Read more >
API — Jinja Documentation (3.0.x)
A callable that can be used to process the result of a variable expression before it is output. For example one can convert...
Read more >
Primer on Jinja Templating - Real Python
While you can think of the normal variables as substrings, Jinja blocks wrap a larger section of your template. That's why you also...
Read more >
Implementation of Jinja2 Variables · Issue #217 · python-eel/Eel
One of the features I find lacking is the implementation of Jinja Variables. While the documentation and the options allow for inserting and ......
Read more >
Scopes and Variable Behavior — Jinja Documentation
This implementation was improved in Jinja 1.1. In Jinja 1.0 blocks that were not top-level were not propagated to the layout template. This...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found