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