Home > Net >  How do I use Flask to create a server-sided stopwatch/counter?
How do I use Flask to create a server-sided stopwatch/counter?

Time:09-30

What I want to get is the time since the server started/went live.

from flask import Flask, render_template
from threading import Thread
import random,os,time,datetime

app = Flask(__name__)

@app.route('/')
def main():
  d = datetime.datetime.utcnow()
  jstime = int(time.mktime(d.timetuple())) * 1000 # this obviously doesn't work
    return render_template('index.html', owner=os.environ['REPL_OWNER'], name=os.environ['REPL_SLUG'], timeStart=jstime)

def run():
    app.run(host="0.0.0.0", port=random.randint(1000, 9999))
    
def keep_alive():
    server = Thread(target=run)
    server.start()

e.g if the current date the server went live was January 1, 1970, the webpage would display how many hours/minutes/seconds since that date. I am running hosting everything on replit.

CodePudding user response:

Move

d = datetime.datetime.utcnow()

Outside your route. That way the variable d is a global variable that is only read once.

  • Related