Python Forum
Web App That Request Data from Another Web Site every 12-hours
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Web App That Request Data from Another Web Site every 12-hours
#12
(Sep-24-2018, 04:23 PM)nilamo Wrote: You need to save it somewhere first. If it isn't that important to you, you can store it in memory and make it a global variable, and then pass it to the template like how was shown earlier with the location parameter. Something like this:
from flask import Flask, render_template, jsonify from apscheduler.schedulers.background import BackgroundScheduler import random import requests app = Flask(__name__) content = None def parse_func(): global content response = requests.get('https://nghttp2.org/httpbin/get') r = response.json() lst = [r['url'], r['origin']] rand_value = random.choice(lst) content = rand_value @app.route("/") def template(): global content return render_template('sh2.html', location=content) if __name__ == '__main__': scheduler = BackgroundScheduler() scheduler.add_job(parse_func, 'interval', seconds=15) scheduler.start() app.run(debug=True)
Thank you very much! Is it ok to use global variables for such applications or there are any other approaches?
Reply


Messages In This Thread
RE: Web App That Request Data from Another Web Site every 12-hours - by jomonetta - Sep-24-2018, 04:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to scraping data from dinamic site sergio21124444 2 780 Nov-08-2023, 12:43 PM
Last Post: sergio21124444
  POST request with form data issue web scraping hoff1022 1 2,742 Aug-14-2020, 10:25 AM
Last Post: kashcode
  Scraping a dynamic data-table in python through AJAX request filozofo 1 3,936 Aug-14-2020, 10:13 AM
Last Post: kashcode
  How to retrieve data from site ROHK 2 2,491 Mar-01-2019, 12:26 PM
Last Post: ROHK
  Mechanize and BeautifulSoup read not correct hours vaeVictis 5 4,522 Jan-15-2019, 01:27 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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