Forums

Datetime will not be updated each time I refresh the page

Hello everyone,

I want to deploy a simple project to Pythonanywhere, in the code that will be mentioned, the datetime won't be updated even if I state it in the function. There was a solution I found, and they said "you need to mention datetime within the function not module level" (https://www.pythonanywhere.com/forums/topic/11690/), but although I stated it as advised, still cannot get updated datetime in the key value of timestamp. I can get updated datetime results in my local machine when I send a request with the postman, but couldn't get the same result when I deploy it via Pythonanywhere.

Here is my code:

from datetime import datetime

import pandas as pd
from flask import Flask, jsonify
app = Flask(__name__)


df = pd.DataFrame({"name":["iss"], "id":[25544], "latitude":[-37.154849142298], "longitude":[28.986038357665],"altitude":[433.50271238095],"velocity":[27537.123944258],"visibility":["daylight"],"footprint":[4575.5309717821],"timestamp":[1680003604],"daynum":[2460031.8280093],"solar_lat":[2.9303189144016],"solar_lon":[63.217224261698],"units":["kilometers"]})

df["latitude"] = df["latitude"].astype("float64")
df["longitude"] = df["longitude"].astype("float64")
df["altitude"] = df["altitude"].astype("float64")
df["velocity"] = df["velocity"].astype("float64")
df["footprint"] = df["footprint"].astype("float64")
df["daynum"] = df["daynum"].astype("float64")
df["solar_lat"] = df["solar_lat"].astype("float64")
df["solar_lon"] = df["solar_lon"].astype("float64")

df_p = df.T.to_dict('dict')


@app.route('/', methods=['GET'])
def api_all():
    with app.app_context():
        current_date_time = datetime.now()
        df_p[0].update({'timestamp': current_date_time})
        return jsonify(df_p[0])


app.config['JSON_AS_ASCII'] = False

wsgi config:

import sys

path = '/home/mertyilmaz/geoevent-test' if path not in sys.path: sys.path.append(path)

from app import api_all application = api_all()

What am I doing wrong, the code working fine but cannot update the datetime, it only updates when I reload the page.

Thanks in advance.

When you say "reload the page" do you mean that you need to reload the web app from the configuration page or that you need to reload the page in your browser?

Thank you so much for your rapid reply, dear glenn.

I meant to say reload the page in browser, I also tried to send get request via postman which I think they are the same. The result only changes when I reload the web app from the configuration page. However, this is not feasible. What I want is to get a unique timevalue as much as the browser pages are being refreshed or getting different datetime values after each postman get request.

Am I missing something?

Take a look at https://help.pythonanywhere.com/pages/GlobalStateAndWebApps/