1

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.

Yasmina
  • 51
  • 3
  • It's not really possible to add Python code to HTML files, Javascript is the only language that can easily be run from within a web page. You can either start a Python server locally alongside the HTML pages, or you might need to look into reimplementing your search with a JS library like http://elasticlunr.com/ instead of Whoosh. – Anentropic Mar 12 '21 at 16:17

1 Answers1

0

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.

Allan Simonsen
  • 1,242
  • 4
  • 22
  • 37