Python Forum

Full Version: Buttons in table. Which row was selected to export.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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']
            #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>
{% 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
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>
{% endfor %}
Or use JQuery to get the content of the row and pass it to the backend (AJAX?).
Just a start...

{% 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>
{% endfor %}
$(".export_pdf").click(function() {
    var $tds = $(this).closest("tr").find("td");
    $.each($tds, function() {
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
another option is to send back the info about the pdf you want to export
Could you elaborate what you mean by 'send back the info' something like what gontajones had in their first answer?
(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
Ah np :)

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

<form action='/download_pdf/'>
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 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 = %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 - - [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
Output: - - [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:
[06/06/2018 10:49:44] "POST /login/ HTTP/1.1" 302 0
Pages: 1 2