Python Forum
Buttons in table. Which row was selected to export.
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Buttons in table. Which row was selected to export.
#1
I am using a flask model with sqlalchemy.

The following python code exports a pdf.
@pdf_display_blueprint.route('/download_pdf', methods=['POST' , 'GET'])
def download_pdf():

            selected = request.form['selected']
            selected_row= pdf_store.query.filter.by(export=selected)
            #Get export from a button click, mostlikely going to need to be done by a form of some sort.
            Target_SN = selected_row.product_serial_number

            Target_Doc = selected_row.doctype

            file_data = BytesIO( selected_row.first().data())

            return send_file(file_data, attachment_filename= Target_SN + ' - ' + Target_Doc + '.pdf', as_attachment=True)
            return redirect(url_for('pdf_display.pdf_display_layout'))
This html code displays the rows of the table:

{% for pdf_store in display_pdf_store %}
<tr onclick='highlight()'>
<td> {{ pdf_store.product_serial_number}} </td>
<td> {{ pdf_store.doctype}} </td>
<td> {{ pdf_store.date_time}} </td>
<td> {{ pdf_store.place_of_procedure}} </td>
<td><form action='/download_pdf'><input type='button' id='Export' value='Export'></input></form></td>
</tr>
{% endfor %}

This table will contain many rows, what i want to do is have it so that when you click the 'Export' button on a certain row you get the correct pdf. Does anyone know how I can link the two?

The current setup I have appears to do nothing
Reply
#2
I think you'll have to do something like:

{% for pdf_store in display_pdf_store %}
<tr onclick='highlight()'>
<form action='/download_pdf'>
<td> {{ pdf_store.product_serial_number}} <input type="hidden" name="product_serial_number" value="{{ pdf_store.product_serial_number}}" /> </td>
<td> {{ pdf_store.doctype}} <input type="hidden" name="doctype" value="{{ pdf_store.doctype}}" /> </td>
<td> {{ pdf_store.date_time}} <input type="hidden" name="date_time" value="{{ pdf_store.date_time}}" /> </td>
<td> {{ pdf_store.place_of_procedure}}  <input type="hidden" name="place_of_procedure" value="{{ pdf_store.place_of_procedure}}" /></td>
<td><input type='submit' id='Export' value='Export'></input></td>
</form>
</tr>
{% endfor %}
Or use JQuery to get the content of the row and pass it to the backend (AJAX?).
Just a start...

HTML:
{% for pdf_store in display_pdf_store %}
<tr onclick='highlight()'>
<td> {{ pdf_store.product_serial_number}}</td>
<td> {{ pdf_store.doctype}}</td>
<td> {{ pdf_store.date_time}}</td>
<td> {{ pdf_store.place_of_procedure}}</td>
<td><button type="button" value="export_pdf" class="export_pdf" /></td>
</tr>
{% endfor %}
JS:
$(".export_pdf").click(function() {
    var $tds = $(this).closest("tr").find("td");
    $.each($tds, function() {
        alert($(this).text());
    });
});
Reply
#3
Thanks for the guidance, this gives me another area to continue my trouble shouting with as I had previously only really thought of using Python as my JS is not that great.

Guess its time to brush up on my skills :P
Reply
#4
another option is to send back the info about the pdf you want to export
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Could you elaborate what you mean by 'send back the info' something like what gontajones had in their first answer?
Reply
#6
(Jul-06-2018, 12:01 PM)KirkmanJ Wrote: Could you elaborate what you mean by 'send back the info' something like what gontajones had in their first answer?
My bad, I misread @gontajones - that's what he suggests in the top snippet in hos post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Ah np :)

To note Jones first code looks promising however it throws up a 400 error im going to have to investigate
Reply
#8
I forgot the final /:

<form action='/download_pdf/'>
Reply
#9
As with everything to do with codding you solve one error and another takes its place ( normally a larger one. )

404 - Error: File not found.

2018-07-06 13:57:45,970 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'
2018-07-06 13:57:45,970 INFO sqlalchemy.engine.base.Engine ()
2018-07-06 13:57:45,976 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()
2018-07-06 13:57:45,976 INFO sqlalchemy.engine.base.Engine ()
2018-07-06 13:57:45,977 INFO sqlalchemy.engine.base.Engine show collation where Charset = 'utf8' and Collation = 'utf8_bin'
2018-07-06 13:57:45,977 INFO sqlalchemy.engine.base.Engine ()
2018-07-06 13:57:45,980 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1
2018-07-06 13:57:45,980 INFO sqlalchemy.engine.base.Engine ()
2018-07-06 13:57:45,982 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1
2018-07-06 13:57:45,983 INFO sqlalchemy.engine.base.Engine ()
2018-07-06 13:57:45,984 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin AS anon_1
2018-07-06 13:57:45,984 INFO sqlalchemy.engine.base.Engine ()
2018-07-06 13:57:45,988 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2018-07-06 13:57:45,990 INFO sqlalchemy.engine.base.Engine SELECT user_info.id AS user_info_id, user_info.username AS user_info_username, user_info.location AS user_info_location, user_info.password_hash AS user_info_password_hash, user_info.is_admin AS user_info_is_admin
FROM user_info
WHERE user_info.id = %s
2018-07-06 13:57:45,990 INFO sqlalchemy.engine.base.Engine (1,)
2018-07-06 13:57:46,006 INFO sqlalchemy.engine.base.Engine ROLLBACK
127.0.0.1 - - [06/Jul/2018 13:57:46] "GET /download_pdf/?product_serial_number=hi&doctype=hi&date_time=&place_of_procedure=admin HTTP/1.1" 404
Reply
#10
Output:
127.0.0.1 - - [06/Jul/2018 13:57:46] "GET /download_pdf/?product_serial_number=hi&doctype=hi&date_time=&place_of_procedure=admin HTTP/1.1" 404
This came after you clicked the form (row) submit button?
It must be a POST not a GET.

Just for example, I have a login app and when a click to Login it goes like this:
Output:
[06/06/2018 10:49:44] "POST /login/ HTTP/1.1" 302 0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask export/upload database table in cvs/xlsx format steve87bg 4 6,724 Jun-19-2020, 01:46 PM
Last Post: steve87bg
  Want to scrape a table data and export it into CSV format tahir1990 9 5,094 Oct-22-2019, 08:03 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020