I am trying to build a front end for a simple TFIDF based document retrieval model(all written in python). The front end will be a simple search bar where the user can enter a query. Using that query I want to return the documents ranked on the basis of their relevancy. I have the backed ready. I have a small function, lets call it query_scorer that takes in the query, does the requisite pre-processing(tokenization, spellcheck, lower casing, etc.) and selects and ranks documents based on their relevancy. What I don't know is how do I pass this query from my html page to the query_scorer and pass the results back to the html page (or maybe a different html page). Lets say I have the following page.
<section >
<form action="" method="">
<input type="search" placeholder="What are you looking for?">
<button>Search</button>
</form>
</section>
How do I transfer the text from the search box to my python script?
Try this:
In the form tag's action="",provide the location of your cgi script and the value of the textbox will be passed to the cgi script.
eg.
<form name="search" action="~/query_scorer.py" method="get">
<input type="text" name="searchbox">
<input type="submit" value="Submit">
</form>
query_scorer.py
import cgi
form = cgi.FieldStorage()
searchterm = form.getvalue('searchbox')
Hope so you may get your result.
You will need to host the php script and expose it as either a web service or web page. I would suggest web page as the easiest method to get started.
You will then need to post to this web page from your form above by entering the action and method in your form attributes.
You web page will need to return html and also call your function.
See a basic overview here
Related
I have a Vue-multiselect, as in this example (not mine)
https://jsfiddle.net/gmsa/04jvjuv3/
I'm trying to get the value in the multi select element and POST it back to my Flask app.
HTML
<form action="" method="POST">
<multiselect id="multi", name = "multi" :multiple="true" :hide-selected="true" :selected="selected" :options="options" :taggable="false" #update="updateSelected"></multiselect>
<button type="submit" value="l1" >Load</button>
</form>
Flask
if request.method == 'POST':
print request.form.getlist('multi')
#prints an empty list
print request.form['multi']
#hits a bad request error
I've been klutzing about with this for a long time now - why can't I get the value from the form element?
The thing is that vue-multiselect is meant to be handled using pure javascript. So, you could just use the solution implemented by the user in that issue.
In HTML:
<input style="display: none;" :value="selected" name="multiselect">
Inside the form, and in python:
request.form.get('multiselect').split(',')
The split is in case multiple options were selected.
BTW, that fiddle uses old versions of both vue and multiselect, but that's unrelated.
I wish to allow users on my site to add PayPal hosted buttons to personalised web pages hosted on it.
Since allowing them to upload the HTML code of the button is not easy to secure against XSS (that is - writing a strict whitelist validation of the HTML format), I'm looking for a way to get the HTML code from PayPal itself using the hosted button ID (which the users will upload, instead of the raw HTML code).
Does anyone know if PayPal cater to this need? If not - do you know of an easy way for me to validate the HTML (preferably in python) as something I can safely load to a customised web page?
Thanks,
-Lior
I believe that with the button ID is enough. What else you need? With the button ID you can build the whole HTML button code.
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="{ID GOES HERE}">
<input type="image" src="https://www.paypalobjects.com/es_ES/ES/i/btn/btn_donateCC_LG.gif" border="0" name="submit">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
There is a PayPal API called "Button Manager API Operations" which will allow you to obtain button information if you have the ID. However, the caller must have the credentials of the account of the hosted button. Here you can find information about it:
General Documentation
You will need specifically the call "BMGetButtonDetails"
BMGetButtonDetails Documentation
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.
how to create a html text link that delivers a KNOWN string to a python file, then redirects you to that python file?
i'm thinking somthing like this
lets say the string i want to deliver is 'lolipop'.
where would it fit
<form action="file.py" method="get">
<input ????? />
</form>
With a hidden html input object.
http://www.w3schools.com/jsref/dom_obj_hidden.asp
I'm trying to get to grips with web2py/python. I want to get the user to fill in a search form, the term they search for is sent to my python script which should send the query to the blekko API and output the results to them in a new HTML page. I've implemented the following code but instead of my normal index page appearing, I'm getting the html response directly from blekko with '%(query)' /html appearing in it's search bar. Really need some help with this!
HTML form on the default/index.html page
<body>
<div id="MainArea">
<p align="center">MY SEARCH ENGINE</p>
<form name="form1" method="get" action="">
<label for="SearchBar"></label>
<div align="center">
<input name="SearchBar" type="text" id="SearchBar" value="" size = "100px"><br />
<input name="submit" type="submit" value="Search">
</div>
</form>
<p align="center"> </p>
Python code on the default.py controller
import urllib2
def index():
import urllib2
address = "http://www.blekko.com/?q='%(query)'+/html&auth=<mykey>"
query = request.vars.query
response = urllib2.urlopen(address)
html=response.read()
return html
I think you are misunderstanding how string formatting works. You need to put the address and query together still:
address = "http://www.blekko.com/?q='%(query)s'+/html&auth=<mykey>" % dict(query=request.vars.query)
Add a hidden field to your form, call it "submitted". Then reformat your controller function as such:
import urllib2
def index():
if request.vars.submitted:
address = "http://www.blekko.com/?q='%(query)'+/html&auth=<mykey>"
query = request.vars.query
response = urllib2.urlopen(address)
html=response.read()
return html
else:
return dict()
This will show your index page unless the form was submitted and the page received the "submitted" form variable.
The /html doesn't do anything. Glad your question got answered. There is python client code for the blekko search api here: https://github.com/sampsyo/python-blekko