Flask rendering previous version of CSS

Viewed 390

Flask app is rendering a previous version of my css file (I have saved and made changes, but when I inspect page, the css file shown is a previous version). Maybe previous version of css file is somehow stuck in the cache? I tried restarting browser, no luck .

Here is the code:

Part of app.py file (the part where I'm rendering the HTML file):

from flask import Flask,render_template,url_for,request
from twilio.rest import Client
app = Flask(__name__, template_folder='templates')
app.static_folder = 'static'
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
import pandas_datareader as pdr
#import yahoo finance api fixer
import fix_yahoo_finance as fyf
from pandas_datareader import data as pdr
from datetime import datetime, timedelta

@app.route('/')
def home():
    return render_template('index.html')

if __name__ == "__main__":
    app.run(debug=True)

index.html:

<!DOCTYPE html>
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}" />
        </head>
        <body>
            <h1>Ticker Predictor</h1>
            <h2>Using Machine Learning to Predict Tomorrow's Stock Prices</h2>
            <br>
            <h3>Simply enter a valid stock ticker below and hit the predict button to receive a text message of tomorrow's predicted opening price of that stock within around 10 minutes!</h3>
            <!-- Main Input For Receiving Query to our ML -->
            <form action="{{ url_for('predict')}}"method="post">
                <input type="text" placeholder="Input Stock Ticker Here" required="required" />
                <button type="submit">Predict</button>
            </form>
            {{ prediction_text }}
        </body>
    </html>

And here is the file structure:

TickerPredictor
    |--static/
        |--styles.css
    |--templates/
        |--index.html
    |--app.py

Any help would be much appreciated! Thank you!

2 Answers

your app.py should be something like this

from flask import Flask,render_template

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

if __name__ == "__main__":
    DEBUG = True
    HOST = '0.0.0.0'
    app.run(debug=DEBUG, host=HOST)

In your app.py file you did not mention the host. Update your app.py file and it should work.

Thanks to help from Rahul and stackoverflow.com/questions/21714653/flask-css-not-updating , I just performed a hard reload in my browser to clear the cache (CMD + SHIFT + R). In other words, the previous version of the css file was getting stored in the browser cache, clearing the cache gets rid of previous css file version and most recent version is then displayed (which is obviously what I want). Thanks everyone!

Related