how to display the results of a python program in a html files output

39 views Asked by At

I'm trying to create an extension that searches google and allows the user to access the first 10 or so results.
I did the google search code in python and have the code for an extension but I don't know how to get the results of the python code and get them to display like text and links in a html document.

I've tried using pycharm but at first the code didn't do anything then I don't know I must touched something then it displayed the python results in the software I was in but didn't in the extension when opened up so I tried using flask but am still unsure of what I'm doing and if it will do what I need it to do.

1

There are 1 answers

0
Mohammed Jhosawa On

Flask allows you to generate HTML dynamically by combining static HTML content with dynamic data using rendering templates.

Refer this doc - https://python-adv-web-apps.readthedocs.io/en/latest/flask3.html

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/search')
def search():
    # results = Get google search results here
    results = "";
    return render_template('results.html', results=results)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Results</title>
</head>
<body>
    <h1>Results</h1>
    <ul>
        {% for result in results %}
        <li>{{result}}</li>
        {% endfor %}
    </ul>
</body>
</html>