Home > Net >  Auto Update Data on Page when Args are changed in URL - Flask Python
Auto Update Data on Page when Args are changed in URL - Flask Python

Time:06-13

I have a flask app that extract 3 values from url in form of arguments like - ticker , start date , end date

The issue that i face is when i first hit the url it returns me data correctly although when i try it next time while changing the values of arguments it doesn't work

Standard form of URL- 127.0.0.1:5000/api?ticker=IEX&start=25-04-2022&end=25-05-2022

Although when i lets say change the start value from 25-04-2022 to 25-03-2022 arguments the code returns the same value as it did for the first one.

Here's my code

import requests
from datetime import date 
import time
import json
import pandas as pd
import os
from flask import Flask
from flask_caching import Cache
from flask import request

config = {
    "CACHE_TYPE": "SimpleCache",  # Flask-Caching related configs
    "CACHE_DEFAULT_TIMEOUT": 0,
}

app = Flask(__name__)
# tell Flask to use the above defined config
app.config.from_mapping(config)
cache = Cache(app)

@app.route("/api")
@cache.cached(timeout=0)
def index():
    # https://www.nseindia.com/api/historical/cm/equity?symbol=IEX&series=["EQ"]&from=25-04-2022&to=25-05-2022
    ticker = request.args.get( 'ticker', None)
    start = request.args.get( 'start', None) #25-04-2022
    end = request.args.get( 'end', None) #25-05-2022
    s = requests.Session()
    headers =   {'Host':'www.nseindia.com', 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0','Accept':'text/html,application/xhtml xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Language':'en-US,en;q=0.5', 'Accept-Encoding':'gzip, deflate, br','DNT':'1', 'Connection':'keep-alive', 'Upgrade-Insecure-Requests':'1','Pragma':'no-cache','Cache-Control':'no-cache',  }
    url = 'https://www.nseindia.com/'
    step = s.get(url,headers=headers)
    api_url = f"https://www.nseindia.com/api/historical/cm/equity?symbol={ticker}&series=["EQ"]&from={start}&to={end}"
    result = s.get(api_url,headers=headers).json()
    return result

if __name__ == "__main__":
    app.run(debug=True, port=int(os.environ.get('PORT', 5000)))

CodePudding user response:

It's probably because you're caching with timeout as 0, so the cache is set for infinite time

@cache.cached(timeout=0)

try commenting out or removing the following lines

cache = Cache(app)
@cache.cached(timeout=0)
  • Related