Home > Net >  how can i show css animation in my html using flask?
how can i show css animation in my html using flask?

Time:12-09

im a begginer here , struggling with basic stuff

I have my flask:

from flask import Flask , render_template , send_file , send_from_directory 

import os
app = Flask(__name__)

#PRIMERA FUNCION
@app.route('/')
def index():
    return  render_template('prueba.html') 

and i have my html :

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>CodePen - The Arrow</title>
  <link rel="stylesheet" href="./style.css">

</head>
<body>
<!-- partial:index.partial.html -->
<div ></div>

<!-- partial -->
  
</body>
</html>

when I open the html file on browser it shows the css , but when I run flask css doesnt show and I cant figure out why!!!

I have try this

@app.route('/css/<path:filename>')
def css_file(filename):
    file_path = os.path.join('css', filename)
    return send_file(file_path) 
   

and i also thought it was a problem of my folder layout but I already tried re arrange folders

CodePudding user response:

Flask has different ways to do this, but the convention is to put static assets (like CSS) in the static directory (which should be at the root of your project) and link them in with Jinja and the url_for() function. so in your case it would look like this:

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>CodePen - The Arrow</title>
  <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>

More explained in the Flask docs

  • Related