Is there a way to link function to button in Flask? - python

I would like to create a button to download the data contained in the table of my HTML page. First of all I thought of saving the data in a table in my database and then create a function that can be called up when the button is clicked (I specify that I would not want to be directed to any page and therefore to remain in the same where the button is). How can I link this function to the button? I tried using onClick() but it doesn't work.
This is the HTML code
<form method="GET" align="center" >
<button onclick="download_file()" id="download-button" class="btn btn-primary btn-lg active" type="submit">Download Table</button>
</form>
Thanks to everyone

Related

How do I select this dropdown button in Selenium?

I have this html :
<div class="dropdown float-right">
<button class="btn btn-default" type="button" id="export-button" data-toggle="dropdown">
<i class="the-download-export"></i>
Export
</button>
<div class="dropdown-menu dropdown-menu-right"><div class="dropdown-item action-export" data-format="csv" data-store="com" data-reverse-type="single"><i class="text-primary fa fa-upload" aria-hidden="true"></i>Download File</div>
This is a dropdown that has a few options. When the button is clicked, it collapses the dropdown selection and the divs appear(there are more divs for various data formats which i haven't added here for simplicity).
I have tried a few things in order to click the button with the csv data format, but everything has failed. Either not finding it or getting the div only, which is not interactible.
What is the proper way of selecting and clicking this dropdown collapsible button in Python ?
That's very little to work on.
I can recommend either checking whether you can get the data another way (e.g. api call when clicking the button) or something with x-path like
button = getFromXPath(".//button[#id='export-button']")
button.click()

How to find out which input type submit button has been clicked? Or how to get the value or name of the clicled button?

So I have a form equipped with several buttons. The form has a textfield which basically shows the contents of the files uploaded by the user. Now clicking on any of these buttons submits the form, but I need to tag the content submitted with a keyword depending on which button was clicked and I need to do this in Python as afterwards these contents are being sent via a queue. And this is all done in Flask. Now my question is: Is there any way to find out which submit button was clicked in python?
If you give your button a name, the clicked one will be sent through as any other input, so you can access it in Flask.
<input type="submit" name="action" value="Create">
Or maybe use the same name with different values.
<input type="submit" name="action" value="Create">
<input type="submit" name="action" value="Delete">
Try this, It gives the element which you clicked.
$(document).on('click', function(event) {
var target;
return target = $(event.target);
});

clicking a button on a password protected website in python

I'm trying to write a script that pushes a button that looks like this in HTML code:
<button type="button" class="button save create">
<img src="img/tick-circle.png" title="" alt="">
Create
</button>
I read this post: Python: clicking a button but I can't figure out how to translate it to my problem because the website I am trying to click a button on does not take me to a new page once the button is clicked. Any suggestions?

Python mechanize click submit on multi submit form

I have to click on the first submit on a multi submit form. The submit code is as below:
<div class="grid ">
<div class="grid__item grid-u-2-10">
<label class="form__label">Auto-Fill:</label>
</div>
<div class="grid__item grid-u-8-10" id="autofill-button-container">
<input type="button" id="autofill" name="autofill" value="Auto-Fill" class="form__input" onclick="AutoFill();" /> Use AutoFill first, as it will replace everything below.
</div>
</div>
I tried:
br.open(url+"upload.php")
br.select_form(nr=7)
r = br.submit(label='Auto-Fill')
however it returns:
mechanize._form.ControlNotFoundError: no control matching kind 'clickable', label 'Auto-Fill'
Pls help.
you're trying to click the wrong button. The input-button action is what you need to go for. Assuming you're selecting the right form. If not br.select_form(nr=8)
Try:
br.submit(name=autofill)

python flask iframe and download

I have to perform two actions based on radio button selection, either download or view a document
<form method="post" action="{{ url_for('page_after_submit') }}">
<p> Your resume </p>
<div class="radio">
<label> <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked> Download document </label>
</div>
<div class="radio">
<label> <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2"> View document </label>
</div>
<input type="submit" />
</form>
My page_after_submit has this code...
#app.route(local.URL_PREFIX + '/page_after_submit/', methods=['POST'])
def after_submit():
if 'option1' == request.form['optionsRadios']:
return/redirect ("download from this url")
if 'option2' == request.form['optionsRadios']:
return/redirect ("view in this iframe")
return (Url_for('go back to submit page if you are here')
I know my form can only have one action which is '/page_after_submit/', what code (HTML or Python in flask) I need to complete rest of my actions ??? I tried to put the iframe tags with complete download file address in the redirect for option2 but doesn't work. I also want this iframe to pop up not open a new browser window. Plus for the download, don't know what to do specially different operating system may have different path for download directory.
My goal is to not have any javascript as well, don't know if it's possible or not. Thanks in advance.
You need to craft a different response depending on how they want to see the data.
Download File
If they choose option 1, you need to set the headers and response to allow the browser to trigger a file download. Here's how you can do something like that.
View File in Iframe
If they choose option 2, you need to return an HTML response which loads the file. This can be done in an Iframe if you'd like, but it's not necessary. Here's one possible way to do that, but many others exist.

Categories