So I had been working on a code for a movie-recommendation site for a school project, which we were supposed to do in Python. It stores a dictionary of movies and takes couple inputs to recommend movies/add new movies etc. I thought of going the extra mile and I designed a html-css website for my code that has a few interlinked pages. The issue is that I can't figure out how to connect my python code to the webpages.
I haven't used web frameworks like Flask before so I can't figure out how to link it to my index.html file. Also, I don't totally get how app routes work.
I tried putting in the file path to my html file in the app route () argument but it resulted in an error. And when I put in my html code in the return value, it didn't gi ve me my designed webpage.
@app.route("/")
def index():
return "Hello World!"
- What does the url in the () exactly do?
- what argument do I pass in the def ()? and what does def actually define?
- How do I integrate my pre-existing python code?
- Where do I put in my html-css code?
So I had been working on a code for a movie-recommendation site for a school project, which we were supposed to do in Python. It stores a dictionary of movies and takes couple inputs to recommend movies/add new movies etc. I thought of going the extra mile and I designed a html-css website for my code that has a few interlinked pages. The issue is that I can't figure out how to connect my python code to the webpages.
I haven't used web frameworks like Flask before so I can't figure out how to link it to my index.html file. Also, I don't totally get how app routes work.
I tried putting in the file path to my html file in the app route () argument but it resulted in an error. And when I put in my html code in the return value, it didn't gi ve me my designed webpage.
@app.route("/")
def index():
return "Hello World!"
- What does the url in the () exactly do?
- what argument do I pass in the def ()? and what does def actually define?
- How do I integrate my pre-existing python code?
- Where do I put in my html-css code?
1 Answer
Reset to default 1"Put your HTML and CSS code into the same directory and use render_template from Flask to serve your HTML files."
your_project/
├── app.py
├── templates/
│ ├── index.html
└── static/
└── style.css
"If you want to pass any argument into the function, first you need to set the route."
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/index/<movies>', methods=['GET'])
def index(movies):
return render_template('index.html', movies=movies)
if __name__ == '__main__':
app.run(debug=True)