Add whoosh search engine to local html files

312 Views Asked by At

I have multiple HTML files that are all stored locally. I wrote a Python script with Whoosh to create an index to search the content of these files. I also have a Python script with Whoosh code to search this index based on a query. Now I want to add the search results into the HTML files. How can I add Python code to local HTML files?

My biggest restriction is that I have no access to a server, everything has to run locally. I need to embed Python code in HTML files without accessing a server.

1

There are 1 best solutions below

0
Allan Simonsen On

You should have a look at Python Flask. It creates a local server that you can server your html from and create JavaScript to call Python methods that access your Whoosh index.

To get started have a look at the minimal Flask application

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

@app.route("/queryIndex/<query>")
def queryWhooshIndex(query):
    # add your code for querying the Whoosh index
    query_result = ""
    return f'\{ "query_result": "{query_result}" \}'

From your html page do a XMLHttpRequest to the url http://127.0.0.1:5000/queryIdex/yourQuery and then parse the returned json.