[Python] FLASK & JINJA2 WITHOUT JS: How can I use jinja2 to create a dynamic dropdown, and...

Discussão em 'Python' iniciado por Stack, Julho 31, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create a dropdown menu, on an html page, that populates different items, depending on the file it reads from. Then, when I select an item from that menu, and press a button, I want that specific selected item to get passed on to be used for the next page (display raw data, chart, etc).

    I am able to create a dynamic dropdown menu, but I can't pass what I select from that menu back to flask to continue my objective. I've tried many variations from what I've shown below. The closest I've gotten is this part below, which ends up populating, and displaying the dropdown list, which appears to be fully functional,
    but not being able to pass a selection back to flask! I see many questions, tutorials, and posts online that i've bookmarked and read, about populating a dropdown with flask and/or jinja2.
    but, never anything that shows a continuation, or solution to pass a selection FROM JINJA2 TEMPLATE AS A VARIABLE BACK TO THE FLASK APP.

    I think this has to be defined in the jinja2 html page. I want it to be defined in the jinja2 html page, if possible without javascript.

    app.py snippets:

    gaaplst = {}
    for ifils in ifilist:
    i = open(ifils, "r")
    result = json.loads(i.read())
    concepts_list = list(result['facts']['us-gaap'].keys())
    gaaplst = concepts_list
    gaap = list(reversed(gaaplst))


    gaapdict = {}
    for ifils in ifilist:
    i = open(ifils, "r")
    result = json.loads(i.read())
    concepts_list = list(result['facts']['us-gaap'].keys())
    gaapdict = concepts_list
    dict = {tuple(gaapdict):1}
    popgaap = (tuple(dict))


    @server.route('/form', methods=['GET', 'POST'])
    def form():
    form_data = {
    'CONCEPTS':{popgaap},
    }
    CONCEPTS = form_data['CONCEPTS']
    if request.method == 'GET':
    form_data = request.form
    return render_template('form.html', form_data=form_data)
    if request.method == 'POST':
    form_data = request.form
    cik = request.form.get('CIK')
    cikchart = request.form.get("CHART")
    concept = request.form.get('CONCEPT')
    if concept:
    return render_template('form.html', form_data=form_data, gaap=gaap)
    if chart:
    .....
    .....
    df = pd.DataFrame(df, columns=['filing_date', selected_concept])


    html page:

    {% extends 'base.html' %}

    {% block content %}
    <h1>{% block title %} Search {% endblock %}</h1>
    <form action="/form" method = "POST">
    <p>CIK: <input name = "CIK" input type = "text"/></p>
    <p>POPULATE: <input name = "CONCEPT" input type = "submit"/></p>
    <br>CHART THE CONCEPT: <select name = "CONCEPT" id="CONCEPT"></br>
    {% for i in gaap %}
    <option value="{{ CONCEPT }}" SELECTED>{{ i }}</option>
    {% endfor %}
    <p>Chart: <input name = "CHART" input type = "submit"</p>
    </select>
    <p>Data: <input name = "RAW" input type = "submit"/></p>
    </form>
    {% endblock %}


    Ive also been able to have the dropdown menu only show one selection, by doing something like: i[:1] (which ends up showing either the first or the last item), but the selection still never gets passed on back to flask. plz help.

    Continue reading...

Compartilhe esta Página