Python Forum
API auto-refresh on HTML page using Flask
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
API auto-refresh on HTML page using Flask
#1
I have been working on an app for a simple radio station app includes real-time updates for show and song information retrieved via API, and have been really stuck on getting the information to automatically update on the HTML page.

What I would like is for the HTML page to update song and show information when they change, according to data retrieved from the spinitron.com API.

I’ve been looking into websockets, which seems to provide the ability for my app to automatically send updates to the HTML page, but I guess I don’t fully understand how the app will know when to send updated information. Like what ‘event’ could trigger it?

How does the app ‘know’ when a song or show has been updated? Do I have to set it up to hit up the API every minute or so, or is there a better way to do it?

Here’s my application.py code:

app = Flask(__name__)

@app.route("/")
def index():


    #Get current show
    show-data = requests.get("https://spinitron.com/api/shows?access-token=my-api-key”)
    show = show-data.json()
    current = show[ "items" ]
    now = (current[0]["title"])
    
    #Get current song playing
    spins = requests.get("https://spinitron.com/api/spins?access-token=my-api-key")
    spin = spins.json()
    spinz = spin[ "items"]
    artist = spinz[0]["artist"]
    song = spinz[0]["song"]
    release = spinz[0]["release"]

    return render_template("index.html", now=now, spinz=spinz, artist=artist, song=song, release=release)
Here's the HTML:

Output:
<head> <title> Radio Station </title> </head> <body> <b>On-Air: </b>{{ now}} <br> <b>Now Playing:</b>{{song}} by {{artist}} from {{release}} <br> </body> </html>
Any help would be greatly appreciated, thanks!
buran write Dec-21-2020, 03:11 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
(Dec-21-2020, 03:06 PM)toc Wrote: How does the app ‘know’ when a song or show has been updated? Do I have to set it up to hit up the API every minute or so, or is there a better way to do it?
Websockets may be overkill for this,as a API dos not usually update so much that need a real time communication.
Here a couple of post you can look at.
So in link1 use APScheduler then just a tricks to reload page from client side.
link2 use Ajax from client side to get vaule from server in a schedule way.
toc likes this post
Reply
#3
(Dec-22-2020, 12:53 AM)snippsat Wrote:
(Dec-21-2020, 03:06 PM)toc Wrote: How does the app ‘know’ when a song or show has been updated? Do I have to set it up to hit up the API every minute or so, or is there a better way to do it?
Websockets may be overkill for this,as a API dos not usually update so much that need a real time communication.
Here a couple of post you can look at.
So in link1 use APScheduler then just a tricks to reload page from client side.
link2 use Ajax from client side to get vaule from server in a schedule way.

Thanks, I'll give both a look! One thing I can't do is refresh the entire HTML page. It's going to have streaming audio, so an entire refresh would restart the audio player. I need to keep the refreshing isolated to the show/song title info.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask - use bootstrap styles in HTML Krayna 1 1,042 Aug-29-2023, 02:33 PM
Last Post: menator01
  Flask run function in background and auto refresh page raossabe 2 7,482 Aug-20-2022, 10:00 PM
Last Post: snippsat
  show csv file in flask template.html rr28rizal 8 34,693 Apr-12-2021, 09:24 AM
Last Post: adamabusamra
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,606 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Parsing html page and working with checkbox (on a captcha) straannick 17 11,240 Feb-04-2021, 02:54 PM
Last Post: snippsat
  Saving html page and reloading into selenium while developing all xpaths Larz60+ 4 4,163 Feb-04-2021, 07:01 AM
Last Post: jonathanwhite1
  Reload flask current page GrahamL 2 5,115 Jan-08-2021, 08:31 AM
Last Post: GrahamL
  Flask formatting into HTML engineer_geek 1 2,771 Aug-05-2020, 01:51 AM
Last Post: engineer_geek
  [Flask]After login page is not redirecting me to dashboard shockwave 0 2,696 May-07-2020, 05:22 PM
Last Post: shockwave
  Flask - adding new page affects all other pages CMR 15 5,592 Mar-28-2020, 04:13 PM
Last Post: CMR

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020