0

I don't have much experience with python and I am looking for a clean way to send a json(which is the result of a query )to the front end and have structred nice.

The query looks like this:

def myquery(time1, time 2):
    query_basictable = """
    Select ...
     my_data = pd.read_sql(sql=query_basictable, con=engine)

    return my_data.to_json()

Now here is where I belive I am mistaking:

df_my_data=myquery(time1, time 2)
data = df_my_data

The json:

data_out = {}
data_out['datas']={}
data_out['datas']['stores']= data

And in the front end:

<td colspan="2" class="{{ data['datas']['stores']}}">
                    <h4><b>Stores</b></h1>
                    <h3>{{ data['datas']['stores'] }}</h3>
                </td>

The result looks like this:

{"Store":{"0":"Store_013","1":"Store_03"}}

What am I doing wrong in order to have something nice like:

Store_013
Store_03
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Christian
  • 459
  • 4
  • 12

1 Answers1

0

The jinja template can receive the dictionary and iterate over it using iteritems() and for loop

<ul>
    {% for key, value in dataout.iteritems() %}
        <li>{{value}}</li>
    {% endfor %}
</ul>

Here dataout is send using render_template() in view function as follow:

@app.route('/yoururl')
def view_name():
    # Other logics here
    return render_template('some.html', dataout=dataout)

For this at least create an empty some.html file (under templates directory)

Nabin
  • 11,216
  • 8
  • 63
  • 98