miguelgrinberg / turbo-flask

Integration of Hotwire's Turbo library with Flask.
MIT License
301 stars 35 forks source link

inject list instead of dictionary #17

Closed NL-TCH closed 2 years ago

NL-TCH commented 2 years ago

i want to inject a list instead of a dictionary, your template is:

@app.context_processor
def inject_load():
    if sys.platform.startswith('linux'): 
        with open('/proc/loadavg', 'rt') as f:
            load = f.read().split()[0:3]
    else:
        load = [int(random.random() * 100) / 100 for _ in range(3)]
    return {'load1': load[0], 'load5': load[1], 'load15': load[2]}
    #this is a dict!

i have it like this:

@app.context_processor
def inject_load():
    tweets=functions.tweets_ophalen()
    return tweets

the function tweets_ophalen() returns a list, but the turbo_flask module does not like it. i get the following error:

10.5.0.2 - - [15/Oct/2021 11:31:06] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Users\-\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 2088, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\-\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 2073, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\-\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 2070, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\-\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 1515, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\-\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\-\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:\Users\-\Documents\GitHub\NS_twitterzuil\Site\main.py", line 25, in klachten
    return render_template('index.html')
  File "C:\Users\-\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\templating.py", line 146, in render_template
    ctx.app.update_template_context(context)
  File "C:\Users\-\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 756, in update_template_context
    context.update(func())
ValueError: dictionary update sequence element #0 has length 3; 2 is required
10.5.0.2 - - [15/Oct/2021 11:31:06] "GET /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
10.5.0.2 - - [15/Oct/2021 11:31:06] "GET /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
10.5.0.2 - - [15/Oct/2021 11:31:06] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 

i really like the concept of turbo-flask but i am not in a position to change the (big) list into a dict. and would like to see an option to parse lists instead of dicts.

thanks in advance!

miguelgrinberg commented 2 years ago

You appear to have a misunderstanding here. Context Processor functions are a feature of Flask that have nothing to do wtih Turbo-Flask. https://flask.palletsprojects.com/en/2.0.x/templating/#context-processors. A context processor must return a dictionary.

You may be able to use something like return {'mylist': list}, or you can also not use context processors and just send what you want to render in the template directly as arguments into the render_template function call.

NL-TCH commented 2 years ago

thanks for the fast reply! yeah that works by setting the list in a 1variable dict like:

@app.context_processor
def inject_load():
    tweets=functions.tweets_ophalen()
    return {'var1': tweets[:]}

and my html like:

<div id="load" class="load">
    <section class="services">
        <div class="container-fluid">
          <div class="row">
                {% for naam,titel,klacht in var1 %}
                    <div class="col-md-4">
                        <div class="service-item first-item" style="cursor: pointer; height: 450px">
                            <div class="icon"></div>
                            <h2>{{ titel }}</h2>
                            <br />
                            <h3><b>{{ naam }}:</b> {{ klacht }}</h3>
                            </p>
                        </div>
                    </div> 
                {% endfor %}
            </div>   
        </div> 
    </section>
</div>