0

I have an HTML webpage. It has a search textbox. I want to allow the user to search within a dataset. The dataset is represented by a bunch of files on my server. I wrote a python script which can make that search. Unfortunately, I'm not familiar with how can I unite the HTML page and a Python script. The task is to put a python script into the html file so, that:

  1. Python code will be run on the server side
  2. Python code can somehow take the values from the HTML page as input
  3. Python code can somehow put the search results to the HTML webpage as output

Question 1 : How can I do this?

Question 2 : How the python code should be stored on the website?

Question 3 : How it should take HTML values as input?

Question 4 : How can it output the results to the webpage? Do I need to install/use any additional frameworks?

Thanks!

3 Answers 3

1

There are too many things to get wrong if you try to implement that by yourself with only what the standard library provides.

I would recommend using a web framework, like flask or django. I linked to the quickstart sections of the comprehensive documentation of both. Basically, you write code and URL specifications that are mapped to the code, e.g. an HTTP GET on /search is mapped to a method returning the HTML page.

You can then use a form submit button to GET /search?query=<param> with the being the user's input. Based on that input you search the dataset and return a new HTML page with results.

Both frameworks have template languages that help you put the search results into HTML.

For testing purposes, web frameworks usually come with a simple webserver you can use. For production purposes, there are better solutions like uwsgi and gunicorn

Also, you should consider putting the data into a database, parsing files for each query can be quite inefficient.

I'm sure you will have more questions on the way, but that's what stackoverflow is for, and if you can ask more specific questions, it is easier to provide more focused answers.

Sign up to request clarification or add additional context in comments.

2 Comments

overkill for what he asked
I don't know what the OP is looking for exactly, but I think it's easier to follow the flask tutorial and build a simple app than using cgi, so we have something to disagree on...
1

I would look at the cgi library in python.

1 Comment

If OP does choose to use cgi, atleast read this first: w3.org/Security/faq/wwwsf4.html
1

You should check out Django, its a very flexible and easy Python web-framework.

1 Comment

and overkill for what he asked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.