2

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?

Clock Slave
  • 7,627
  • 15
  • 68
  • 109
  • You can host your python script as a service using a web server e.g. Tornado in this case and call the endpoint from javascript. – Afaq Mar 14 '17 at 12:07

2 Answers2

2

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.

Ruan
  • 219
  • 5
  • 9
1

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

hairmot
  • 2,975
  • 1
  • 13
  • 26
  • I am using python and I am assuming you mean something like `
    `(correct me if I am wrong). How do I store this string into a variable?
    – Clock Slave Mar 14 '17 at 12:23
  • 1
    apologies, i misread python as php. it makes no difference though. Check out submitting html forms to python script: http://stackoverflow.com/questions/15965646/posting-html-form-values-to-python-script – hairmot Mar 14 '17 at 14:16