How do I connect my pre-exisiting html-css webpages to a python code using Flask? - Stack Overflow

admin2025-04-16  4

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!"
  1. What does the url in the () exactly do?
  2. what argument do I pass in the def ()? and what does def actually define?
  3. How do I integrate my pre-existing python code?
  4. 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!"
  1. What does the url in the () exactly do?
  2. what argument do I pass in the def ()? and what does def actually define?
  3. How do I integrate my pre-existing python code?
  4. Where do I put in my html-css code?
Share Improve this question asked Feb 2 at 2:37 gravitandogravitando 1992 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

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)
转载请注明原文地址:http://anycun.com/QandA/1744810870a87943.html