Skip to content
This repository was archived by the owner on Jun 22, 2025. It is now read-only.
This repository was archived by the owner on Jun 22, 2025. It is now read-only.

How to pass argument to loop via jinja #485

Open

Description

@evheniu

Describe the problem
How do I pass argument b to a for loop to jinja template?

Code snippet(s)
Example:

import eel

eel.init("web")

b = [1,2,3]

eel.start("templates/main.html",size =(350, 400), jinja_templates='templates')
<html>
    <label>
        <div>Example:</div>
        <select name="a">
            {% for a in b %}
                <option value="{{ a }}">{{ a }}</option>
            {% endfor %}
        </select>
      </label>
</html>

Activity

  1. eddyizm commented on Jan 29, 2022

    @eddyizm

    Did you figure this out? This seems essential to use jinja with it but I'm suspecting it is not possible since I don't seen any examples of passing context.

  2. Dan-Adamson commented on May 16, 2022

    @Dan-Adamson

    Any resolution to this problem?

  3. martin-foka commented on Sep 18, 2022

    @martin-foka

    I use Alpine.js as a workaround for my Jinja templates. All you need for your loop are alpine attributes:

    import eel
    
    @eel.expose
    def b():
        return ["1", "2", "3"]
    
    @eel.expose
    def header():
        return "I am header"
    
    eel.init('web')
    eel.start('templates/base.html', jinja_templates='templates')
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Example</title>
      <script type="text/javascript" src="/eel.js"></script>
      <script src="//unpkg.com/alpinejs" defer></script>
    </head>
    <body x-data>
    <h1 x-text="eel.header()"></h1>
    <template x-for="a in eel.b()">
      <p x-text="a"></p>
    </template>
    </body>
    </html>

    Alpine attributes can also be used in Eel apps for: getting variables, modifying python variables, html if statements, showing/hiding DOM elements, event listeners, transitions...
    Is there a better solution to pass variables to HTML?

  4. eddyizm commented on Sep 18, 2022

    @eddyizm

    @maRT-sk That is an outstanding work around. Seems like a good fit as I have heard alpine being very lightweight and minimal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions