2

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!

watersheep23
  • 339
  • 1
  • 3
  • 17

2 Answers2

2

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.

rmb
  • 553
  • 5
  • 15
  • I posted the html file in the description, do I not have that? I think I do, but I could be wrong – watersheep23 Aug 28 '20 at 04:01
  • Sorry my indenting on html was messed up for some reason – watersheep23 Aug 28 '20 at 04:03
  • 1
    what do you get when you open your `localhost`? also can you post what you are getting in your console when you run `app.py` – rmb Aug 28 '20 at 04:14
  • The issue has also changed. I've updated the description again. It's having no trouble finding the html (no 404 not found error), but a previous version of the css file is getting served in the browser (I inspected page, and the css file is a previous version, even though I've saved changes in the file since that previous version). I'll put console output in description as you have asked – watersheep23 Aug 28 '20 at 04:20
  • Actually the console is empty when I run through local host, so no need to post – watersheep23 Aug 28 '20 at 04:21
  • i updated my previous answer please check. – rmb Aug 28 '20 at 04:24
  • if __name__ == '__main__': app.run(debug=True) is already in app.py – watersheep23 Aug 28 '20 at 04:27
  • I tried setting host like you said and error was "0.0.0.0 address already in use, so I tried "127.0.0.1" (which is what it was automatically doing when I had it as if name == 'main': app.run(debug=True), and nothing changed – watersheep23 Aug 28 '20 at 04:29
  • Here's the error it gave me when I did what you said: * Debug mode: on Traceback (most recent call last): File "app.py", line 102, in app.run(debug=True, host='0.0.0.0') File "/Users/jacklunceford/anaconda3/lib/python3.6/site-packages/flask/app.py", line 990, in run run_simple(host, port, self, **options) File "/Users/jacklunceford/anaconda3/lib/python3.6/site-packages/werkzeug/serving.py", line 1030, in run_simple s.bind(server_address) OSError: [Errno 48] Address already in use – watersheep23 Aug 28 '20 at 04:31
  • https://stackoverflow.com/questions/21714653/flask-css-not-updating Aha, this is my exact issue, I assumed it was caching the css in browser – watersheep23 Aug 28 '20 at 04:34
  • Please update your question properly what you have written in your `app.py` and what error you got while running it. Also once restart your system and try again with private/incognito mode in browser to see if the html is loading or you can also maybe try and reinstall flask again. – rmb Aug 28 '20 at 04:35
  • 1
    @jalunceford Yes it is caching issue. But only for development, you can turn off caching in Devtools -> Network -> Disable caching. This works only if the devtools is open. – Rahul Bharadwaj Aug 28 '20 at 04:37
  • Okay I see, but how do I get this to work without having to disable caching? It displayed most recent css when I disabled caching like you expected. Thank you so much for your answer! That solution is a lot easier than the stack overflow article I linked in a previous comment – watersheep23 Aug 28 '20 at 04:49
  • Nice! I did hard reload on browser like one of the answers in that stack overflow link and it worked (CMD + SHIFT + R) – watersheep23 Aug 28 '20 at 04:55
1

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!

watersheep23
  • 339
  • 1
  • 3
  • 17