0

I've created a Flask API which I want to be hit/refreshed at every second.
Following is the code:

from flask import Flask
from datetime import datetime

app = Flask(__name__)

@app.route("/", methods=["GET"])
def home():
    return """Hello World!<br>The current time is {}.""".format(datetime.strftime(datetime.now(), "%d %B %Y %X"))

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

What is desired?
I basically want the html page to update every second, showing the current date & time, ie., updating every second. I haven't added any explicit HTML page for rendering. I don't know HTML and therefore wanted to get this done via python and flask apis.

How can I achieve this? Is there any better way of doing this?

Aman Singh
  • 1,111
  • 3
  • 17
  • 31
  • 1
    This literally has nothing to do with the endpoint. An endpoint just responds to a request. If you want to request the data once per second, you need to send a _request_ once per second, from whatever front-end you will be building. But this is a code smell. Why do you want do do this? It's very likely to be an [XY problem](https://meta.stackexchange.com/q/66377/248627). – ChrisGPT was on strike Apr 05 '20 at 15:12
  • Looking at your code maybe you just need to run a bit of JavaScript in your front-end? Not one that requests data from the back-end, just one that runs in the browser. – ChrisGPT was on strike Apr 05 '20 at 15:14
  • @Chris I wanted to show the current date & time updating live. What i meant asking was is there any way to make a GET request to an endpoint every second from the same program or do i really have to write another program to make that hit every second? – Aman Singh Apr 05 '20 at 15:15
  • @Chris great! Can you tell me how? Or show me where I can learn that? I don't know html or JS, but for now just want to learn how to get this done – Aman Singh Apr 05 '20 at 15:16
  • Then my second comment is relevant. This shouldn't be an API request at all: just use JavaScript. There are a million tutorials out there for showing the current time. Just search for "JavaScript updating clock" or something. – ChrisGPT was on strike Apr 05 '20 at 15:16
  • @Chris so there's no pythonic way of doing it? – Aman Singh Apr 05 '20 at 15:17
  • Python doesn't run in the browser. As I said in my _first_ comment, you could request data from the back-end every second using JavaScript, but that's a poor solution if you just want to show the time. – ChrisGPT was on strike Apr 05 '20 at 15:19
  • 1
    This post will answer your question : https://stackoverflow.com/questions/53111362/fun-clock-streaming-text-with-python-and-flask – Arpit Maiya Apr 05 '20 at 15:22

1 Answers1

1

You can try this one:

from flask import Flask
from datetime import datetime

app = Flask(__name__)

@app.route("/", methods=["GET"])
def home():
    return """
<meta http-equiv="refresh" content="1" /> 
Hello World!<br>The current time is {}.""".format(datetime.strftime(datetime.now(), "%d %B %Y %X"))

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

Change the content attribute inside the meta tag if you want to change the frequency (the number refers to number of seconds)

Gabio
  • 9,126
  • 3
  • 12
  • 32