Home > Net >  Is there a way to add style to flask app?
Is there a way to add style to flask app?

Time:05-04

I'm trying to add css to my html page in Flask, but it doesn't work. I tried the code in online editor and it works.

HTML page (results.html):

<div>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles/results.css') }}">
    <table>
        <thead>
        <tr>
          <th >File name</th>
          <th >Result</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td > 1 </td>
            <td >2</td>
        </tr>
        </tbody>
    </table>
    <br>
    <br>
</div>

This is my css (results.css)

table, td, th {
  border: 2px solid;
}

table {
  width: 100%;
  border-collapse: collapse;
}

.red {
background-color: #EE3209;
}

.yellow {
background-color: #F0E20A;
}

I put css file in "styles" folder, under "static" folder.

Thank's in advance.

CodePudding user response:

first of all you need to set (in your app.py) where is your static folder:

app = Flask(__name__,
        static_url_path='', 
        static_folder='templates/static',
        template_folder='templates')

In my example I have a folder "template" then the "static" folder(path ex: myapp/template/static). Inside "static" I created the "CSS" folder (you can also add libraries to the "static"). Since I have my files in it I'll just call them like this:

<link rel="stylesheet" href="/css/mycssfile1.css">
<link rel="stylesheet" href="/css/mycssfile2.css">

I hope it can help you!

CodePudding user response:

I solved the same problem using: Ctrl Shift R. In this way you perform a hard reload of a web page in Google Chrome.

  • Related