Python Forum
Web App That Request Data from Another Web Site every 12-hours - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Web App That Request Data from Another Web Site every 12-hours (/thread-12978.html)

Pages: 1 2


RE: Web App That Request Data from Another Web Site every 12-hours - nilamo - Sep-24-2018

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)



RE: Web App That Request Data from Another Web Site every 12-hours - jomonetta - Sep-24-2018

(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?


RE: Web App That Request Data from Another Web Site every 12-hours - nilamo - Sep-24-2018

If the server restarts, do you still need access to the data? You'll lose it if it's a global, until the scheduled part runs again.

I don't know what kind of data it is, but databases are built to store things.


RE: Web App That Request Data from Another Web Site every 12-hours - jomonetta - Sep-24-2018

Yes I need to access it. So, I have to build a query for database with SQLAlchemy or something ?


RE: Web App That Request Data from Another Web Site every 12-hours - nilamo - Sep-24-2018

That depends on what the data is. It might make more sense to just store it in a plain text file, and read it once when the server starts up. It's really hard to say what you "should" do, because we really don't have any idea what you're doing, what sort of traffic you're expecting, what the data is, etc.

I mean, you could just do it, and see if you're happy with the performance you're getting.


RE: Web App That Request Data from Another Web Site every 12-hours - snippsat - Sep-26-2018

(Sep-24-2018, 07:56 PM)jomonetta Wrote: Yes I need to access it. So, I have to build a query for database with SQLAlchemy or something ?
You should use Flask-SQLAlchemy it's the common way for database connection in Flask.
It's elegant made and can plug into database of choice.
SQLite is a good choice for most small to middel large task.

To give a demo that build on what we have here,
so now values is send to client side,then take values back to server side.
Where connect to SQLite and place values in database location.db(one file and portable as it's SQLite).
from flask import Flask, render_template, jsonify, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from apscheduler.schedulers.background import BackgroundScheduler 
import random
import requests

app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///location.db'
db = SQLAlchemy(app)

class Loc(db.Model): 
    id = db.Column(db.Integer, primary_key=True)   
    content = db.Column(db.String(4096), nullable=False)

def parse_func():
    response = requests.get('https://nghttp2.org/httpbin/get')
    r = response.json()
    lst = [r['url'], r['origin']]
    rand_value = random.choice(lst)
    #print(rand_value) 
    return(rand_value)    

@app.route("/foo")
def foo():    
    parse = parse_func()    
    loc = Loc(content=parse)                   
    db.session.add(loc)
    db.session.commit()
    return render_template('sh2_base.html', location=parse)  

@app.route('/db_save')
def db_save():
    my_var = request.args.get('my_var', None)
    loc = Loc(content=my_var)                   
    db.session.add(loc)
    db.session.commit()  
    #print(my_var)
    return redirect(url_for('foo'))

if __name__ == '__main__':
    scheduler = BackgroundScheduler()
    scheduler.add_job(parse_func, 'interval', seconds=15)
    scheduler.start()
    app.run(debug=True)
sh2_base.html:
<!doctype html>
<html>
<head>
  <title>Some title</title>
  <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}" />
</head>
<body>
  <a href="{{ url_for('db_save', my_var=location) }}">Send my_value</a>
  <p>{{ location }}</p>
</body>
<script> 
    function timedRefresh(timeoutPeriod) {
      setTimeout("location.reload(true);",timeoutPeriod);
    }    
    window.onload = timedRefresh(15000);   
</script>
</html>