8000 GitHub - Mosallamy/FLASK-challenges: This repo is a collection of the issues I faced when I started learning Flask.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Mosallamy/FLASK-challenges

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 

Repository files navigation

FLASK Challenges

This repo is a collection of the issues I faced when I started learning Flask.

It's meant to help beginners avoid the HELL of trying to solve problems at the start of learning a new framework.

Topics covered:

Database connections

PDF generation

Runtime errors

Jinja

Global variables

Unfortiannely Jinja does not support global variables. The below example shows the issue:

{% set sum = 0 %}
{{ sum }} {# sum = 0 #}

{% for i in range(1,6) %}  
	{% set sum = sum +  i %}  
{% endfor %}

{{ sum }} {# sum = 0 instead of 15 #}

There are two ways around this limitation:

  1. Using namespace (Added in version 2.10) 1.1 Example 1: summing numbers in a loop
    {% set global_var = namespace(sum=0) %}  
    {{ global_var.sum }} {# sum = 0 #}
    
    {% for i in range(1,6) %}  
        {% set global_var.sum = i + global_var.sum %}  
    {% endfor %}
    
    {{ global_var.sum }} {# sum = 15 #}
    1.2 Example 2: global flag
    {% set global_var = namespace(flag=false) %}  
      
    {% for i in range(1,6) %}  
        {% if i == 4 %}  
            {% set global_var.flag=true %}  
        {% endif %}  
    {% endfor %}  
      
    {% if global_var.flag %} 4 was found! {% endif %} {# Condition is true #}

Added in version 2.10

  1. Using global list
{% set global_list = [] %}  
{{ global_list }} {# global_list = [] #}  

{% for i in range(1,6) %}  
    {% if global_list.append(i) %}{% endif %}  
{% endfor %}  
  
{{ global_list }} {# global_list = [1, 2, 3, 4, 5] #}

Resources you will benifit from

License

License: CC BY 4.0

About

This repo is a collection of the issues I faced when I started learning Flask.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0