[Flask] Creating a bootstrap table with values from mysql using flask

Anthony Ford ford.anthonyj at gmail.com
Tue Apr 19 11:10:15 EDT 2016


Two ways to achieve that (though I'm sure there are about a thousand others
just as good):

One way is to just POST to SingleView, and if there's no POST (i.e. the
"Else" branch of the "if request.method"), render a form page with no
table. If you do have a POST, render the table and data, or a "No Results"
row in the table if there's no data (i.e. a search through the DB with no
results).

Another is to use jQuery to run an AJAX request in the background, to a
separate endpoint. This endpoint (i.e. "/search"), could respond in JSON,
and you have jQuery code process the JSON object and build the table. If
you go this route, you have to have jQuery capture the click on the
"Submit" button, so it doesn't send the form. But this approach means that
someone with NoScript or Javascript turned off wouldn't be able to use your
form.

Both approaches are completely valid. I've used both in different projects,
and even both approaches in the same project. It all depends on what you
want. The first approach is less... elegant if you will. It will involve a
page refresh, and if you are hitting the DB hard (i.e. large query or
search through large number of records), the page refresh could seem slower
to the user than the jQuery approach. The second is more work and involves
more working parts (original view function, ajax endpoint, jQuery code),
and if you are unfamiliar with jQuery and JSON, may have a bit of a
learning curve on top of the Python/Flask stuff.



Anthony Ford,
KF5IBN,
ford.anthonyj at gmail.com

On Tue, Apr 19, 2016 at 10:53 AM, isaac tetteh <itetteh34 at hotmail.com>
wrote:

> Thanks I think I corrected the spelling. But also what i am trying to do
> is click a button and when the button is clicked a jQuery code will slide
> down the table which is is the form tag and the. Access the data from
> app.run. What do you suggest the validation of the button click should be
> other thanif request.method=="POST". Is there a better way to do that.
>
> Sent from my iPhone
>
> On Apr 19, 2016, at 9:36 AM, Oleksandr Chalyi <kerriden1 at gmail.com> wrote:
>
>  if request.method=='POst'
>
> I believe it should be changed to ==  'POST'
>
>  c.execut('''SELECT * FROM Continent''')
> change to
>  c.execute('''SELECT * FROM Continent''')
>
> But actually, the problem here is that if a request method is not POST,
> flask goes to return render_template("view.html", data=data), where
> variable data is not declared.
> You should declare data before  "if request.method == 'POST'"
>
>
> 2016-04-19 16:48 GMT+03:00 Unai Rodriguez <unai at sysbible.org>:
>
>> Don't you want to use flask SQL alchemy? The code becomes much cleaner
>> and less error prone.
>>
>> -- unai
>>
>> On Tue, Apr 19, 2016, at 09:38 PM, isaac tetteh wrote:
>> > hello guys I am trying to create a bootstrap table with data from MySQL
>> > database. I am getting an error local variable data referenced before
>> > assignment. Please help my code is as below.
>> >
>> > app.py code
>> >
>> >     @app.route("viewSingle", methods=['POST','GET'])
>> >     def Singleview():
>> >         if request.method=='POst':
>> >             if request.form['submit']=="view continent':
>> >                 try:
>> >                     c,conn=connection()
>> >                     c.execut('''SELECT * FROM Continent''')
>> >                     data=c.fetchall()
>> >                 except Error:
>> >                     flash("something wrong happend")
>> >                 finally:
>> >                     c.close()
>> >                     conn.close()
>> >         return render_template("view.html", data=data)
>> > view.html
>> >
>> >     <div class= "container">
>> >     <form class=form-group" action="{{url_for('SingleView')}}" method =
>> >     POST>
>> >      <tables class="table">
>> >       <thead>
>> >          <tr>
>> >           <th>header1</th>
>> >           <th>header2</th>
>> >          </th>
>> >        </thead>
>> >       {%for row in data%}
>> >         <tbody>
>> >           <tr class ="success">
>> >             <td>{row[1]}</td>
>> >             <td>{row [1]}</td>
>> >           </tr>
>> >         </tbody>
>> >        </table>
>> >     </form>
>> >     </div>
>> >
>> > It seems to not be working I was hoping on getting the value [0] and [1]
>> > from the tuple( data= c.fetchall()) into the table but I get the error
>> > local variable 'data' referenced before assignment.But if I change the
>> > return render_template("view.html", data=data) to he return
>> > render_template("view.html") its runs with no error but just that the
>> > </td></td>is empty noting print. Is there a better way to do this or am
>> I
>> > missing something. Please advice or help. Thanks
>> >
>> >
>> > Sent from my iPhone
>> > _______________________________________________
>> > Flask mailing list
>> > Flask at python.org
>> > https://mail.python.org/mailman/listinfo/flask
>> _______________________________________________
>> Flask mailing list
>> Flask at python.org
>> https://mail.python.org/mailman/listinfo/flask
>>
>
>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160419/500cd5a9/attachment-0001.html>


More information about the Flask mailing list