Tarun
Tarun

Reputation: 9

How can I combine a HTML and Python File?

I recently did a project where I have to get the following input from the user: Date in DD form, Month in MM form, Year in YY form, Century (19, 20 0r 21) and the date format(MM-DD-YYYY, DD-MM-YYYY, YYYY-MM-DD). And I have to print the date in the format chosen by the user... Further I have to ask the user the number of days to be added to the date that was printed previously and then add the no. of days and then print it and if the user should be asked if he wants to continue adding days, if they choose yes.. again they'll be asked the no. of days to be added, if they select no then program ends there.... Now I have made a HTML files for that... The problem is I don't know how to combine the Python and HTML files... Please help me in this regard.

Upvotes: 1

Views: 8092

Answers (2)

math scat
math scat

Reputation: 310

Use Flask: Install with pip:

pip install flask

Python:

from flask import Flask, render_template
app = Flask(__name__)

@app.route("/")
def home():
    return render_template("HTML_FILE_NAME.html", inp=YOUR_INPUT_VARIABLE)


if __name__ == '__main__':
    app.run(debug=True)  #Run app

In the html file: Where ever you want to use the variable, put {% inp %} Example:

<html>
<head>
<title>My html File</title>
</head>
<body>
<h1>{% inp %}</h1>
</body>
</html>

This will run in your local server by default. Go to: http://127.0.0.1:5000/

Upvotes: 3

K. Karatopcu
K. Karatopcu

Reputation: 49

The keywords you should be looking are a web framework to host your application such as Flask, Django, and a template language to combine python and HTML to use it via these frameworks, such as Jinja2 or Django's own template language. I suggest Flask with Jinja2 since it's a micro framework and easy to start with.

Upvotes: 1

Related Questions