Updating the webpage using Flask based on MySQL database. - python

I have a webpage (built using HTML & jQuery) which displays the data from a MySQL database. I am using Flask to connect HTML with my database. However, my database gets updated every 15 minutes (using a separate Python Script). Currently, I stop the flask server, update the database and restart the Flask to update the webpage. My question is the following:
Is there a way to update the MySQL database in the background without having to stop the flask server? I read about concepts of AJAX and CRON, however i am not able to understand how to use them with flask asynchronously.
Note: I am a newbie in web applications and this is my first project which involves connecting client side and server side. Any help will be appreciated.
Thanks

You are most likely doing something like this:
from flask import Flask, render_template
from yourMySqlLibrary import connect_to_mysql
conn = connect_to_mysql()
# This is only executed when you start the script
data = conn.execute("SELECT * FROM MySemiRegularlyUpdatedTable")
app = Flask(__name__)
#app.route("/")
def view_data():
return render_template("view_data.html", data=data)
if __name__ == "__main__":
app.run()
If that is the case, then your solution is simply to move your connection and query calls into your controller so that the database is re-queried every time you hit the page:
#app.route("/")
def view_data():
# Removed from above and placed here
# The connection is made to the database for each request
conn = connect_to_mysql()
# This is only executed on every request
data = conn.execute("SELECT * FROM MySemiRegularlyUpdatedTable")
return render_template("view_data.html", data=data)
This way, your view will update when your data does - and you won't have to restart the server just to pick up changes to your data.

Related

How to schedule a function in Flask App daily at every 5 minutes in Python

I would like to know how the check function can be executed everyday at 5 minutes interval.
import time
from flask import Flask
def check():
print(time.time())
app = Flask(__name__)
if __name__ == '__main__':
app.run()
Update
I tried the below but nothing happended.
from apscheduler.triggers.combining import AndTrigger
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.triggers.cron import CronTrigger
trigger = AndTrigger([IntervalTrigger(minutes=5),
CronTrigger(day_of_week='mon,tue,wed,thu,fri)])
scheduler.add_job(job_function, trigger)
you need to schedule a Cron job with Crontab if you are planing to deploy your Flask app on unix-like server. for windows server i have no clue how to do that but for sure you can plan tasks.
try python-crontab package if you need to manipulate dynamically your crontab, it's not mandatory for your case if you have the needed permissions on hosting server to insert an entry in user crontab file
few resources that may help you affording this
https://en.wikipedia.org/wiki/Cron
https://stackabuse.com/scheduling-jobs-with-python-crontab/ (tutorial about the python-crontab)
https://medium.com/#gavinwiener/how-to-schedule-a-python-script-cron-job-dea6cbf69f4e
you need a separate and standalone python script apart from your Flask app since cron can't execute a remote http request like curl / wget only shell/python/.. scripts, commands .., meaning in your case you can't insert https://myflaskapp.dev/check in crontab like
5 * * * * https://myflaskapp.dev/check
but the work around is that your cron job execute standalone python script which send/execute an http request to that endpoint like :
5 * * * * /home/webapps/www/myflaskapp.dev/check.py
in check.py you can put simple python script using requests library that just make an http request (get,post ..) depending on your logic in check function
# importing the requests library
import requests
# endpoint
URL = "http://myflaskapp.dev/check"
# defining a params dict for the parameters to be sent to the API (Optionnal)
PARAMS = {'param1': param1}
# sending get request and saving the response as response object
r = requests.get(url = URL, params = PARAMS)
[...]

Trading view alerts to trigger market order through python and Oanda's API

I'm trying to trigger a python module (market order for Oanda) using web hooks(from trading view).
Similar to this
1) https://www.youtube.com/watch?v=88kRDKvAWMY&feature=youtu.be
and this
2)https://github.com/Robswc/tradingview-webhooks-bot
But my broker is Oanda so I'm using python to place the trade. This link has more information.
https://github.com/hootnot/oanda-api-v20
The method is web hook->ngrok->python. When a web hook is sent, the ngrok (while script is also running) shows a 500 internal service error and that the server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
This is what my script says when its running (see picture);
First says some stuff related the market order then;
running script picture
One thing I noticed is that after Debug it doesn't say Running on... (so maybe my flask is not active?
Here is the python script;
from flask import Flask
import market_orders
# Create Flask object called app.
app = Flask(__name__)
# Create root to easily let us know its on/working.
#app.route('/')
def root():
return 'online'
#app.route('/webhook', methods=['POST'])
def webhook():
if request.method == 'POST':
# Parse the string data from tradingview into a python dict
print(market_orders.myfucn())
else:
print('do nothing')
if __name__ == '__main__':
app.run()
Let me know if there is any other information that would be helpful.
Thanks for your help.
I fixed it!!!! Google FTW
The first thing I learned was how to make my module a FLASK server. I followed these websites to figure this out;
This link helped me set up the flask file in a virtual environment. I also moved my Oanda modules to this new folder. And opened the ngrok app while in this folder via the command window. I also ran the module from within the command window using flask run.
https://topherpedersen.blog/2019/12/28/how-to-setup-a-new-flask-app-on-a-mac/
This link showed me how to set the FLASK_APP and the FLASK_ENV
Flask not displaying http address when I run it
Then I fixed the internal service error by adding return 'okay' after print(do nothing) in my script. This I learned from;
Flask Value error view function did not return a response

Web server in python for responding to GET and POST

I want to create web server, and listen now.
Server must have different functions for each endpoint (and method).
I want to get (e.g. to variable) parameters (and data if POST)
Respond to get (and POST if its possible)
Respond in JSON
Someone can help me with this?
PS: I will be run it on Heroku, and send requests to it via Roblox's HttpService
Below see examples of each of your requirements using the Flask lightweight web framework.
After that is a link to a short description of how to deploy to Heroku.
# app.py
from flask import Flask
from flask import request, render_template
app = Flask(__name__)
#app.route('/test-get-request-parameters')
def test_get_request_parameters():
# 1. different function per endpoint
# 2. GET parameter to variable
# 3. respond to GET
var = request.args.get('some_request_variable')
return render_template('hello_world.html')
#app.route('/test-post-method',methods=['POST'])
def test_post_method():
# 2. receive POST data
# 3. respond to POST
print(request.get_json())
return 'hello, world!'
#app.route('/test-get-or-post', methods=['GET','POST'])
def test_get_or_post():
# 4. respond in JSON
if request.method == 'POST':
d = {'hello':'world'}
return d # this will be JSON response
return render_template('test.html')
To deploy to Heroku you need a Procfile with something like this in it:
web: gunicorn app:app
And you can follow these instructions: https://devcenter.heroku.com/articles/getting-started-with-python

How do I fetch data from API in Flask?

I'm trying to set up a Flask API Server from which I can data from local database via an ongoing HTTP Request to another database.
In the local code, I run a thread that is running and updating the local DB every 1 minute.
app = Flask(__name__)
cached_event_log = None
#app.route('/event_log', methods=['GET'])
def get_event_log():
if cached_event_log != None and .get_latest_event_time == cached_event_log[-1]:
return jsonify(cached_event_log)
#MAKE CONNECTION TO DB AND GET THE DATA
return jsonify(event_log)
if(__name__ == '__main__'):
app.run(Debug=True)
I'm struggling to find a "standard" way to set up A request.
Any opinion would be highly appreciated- Thank you,
You could set up a cron job to run a script at a specified interval
Or use something like Advanced Python Scheduler
https://apscheduler.readthedocs.io/en/3.0/
Advanced Python Scheduler support in Flask
https://github.com/viniciuschiele/flask-apscheduler

Elastic Beanstalk does not work with SQLite

I want to put a very simple website on Amazon EB using Flask framework. The website has a welcome form, it takes two values from the user, reads an SQLite database and shows results on the screen. The website works perfectly on my local machine. The problem is on the Elastic Beanstalk.
I create a zip file with the application.py, the SQLite database, the static folder (for bootstrap) and templates folder (for the two templates) and I upload it on the Elastic Beanstalk. My system is windows, running python 3.6. After the upload, EB gives me green status. I click the link on the EB and takes me to the form. All working good so far. Then when I click on the form the button submit to take me to the results page but instead I receive:
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
I worked the code to identify the step that Amazon EB fails to understand and it seems that the program fails at the line cur.execute('''SELECT a, b, c, d, e, f... which means that the Amazon EB does not see/understand my SQLITE database.
Can someone help?
This is my code for the flask program application.py:
import os
import sqlite3
from flask import Flask, request, render_template
application = Flask(__name__)
#application.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'GET':
return render_template('welcome.html')
elif request.method == 'POST':
conn = sqlite3.connect('Sky.db')
cur = conn.cursor()
weekendid= request.form['weekend']
myData=[]
cur.execute('''SELECT a, b, c, d, e, f, g
FROM Table1 WHERE a = ? ORDER BY g DESC LIMIT 5''', (weekendid,))
row = cur.fetchall()
for i in row:
average_1 = (i[1]+i[3])/2
average_2 = (i[2]+i[4])/2
variable1 = i[5]
variable2 = i[6]
cur.execute('''SELECT * FROM Table2 WHERE a = ?''', (i[0],))
coords=cur.fetchone()
zz = [average_1, average_2,variable1,variable2]
myData.append(zz)
return render_template('where.html', myData=myData)
if __name__ == '__main__':
application.debug = True
host = os.environ.get('IP', '127.0.0.1')
port = int(os.environ.get('PORT', 80))
application.run(host=host, port=port)
First. Be careful when using SQLite on Elastic Beanstalk, as if you change configuration it is likely to kill your instance and redeploy. In your case, it doesn't look like you are actually writing to the database, so that isn't a problem.
First step of finding the error might be going to the Elastic Beanstalk console and clicking Request logs. It's under the logs pane.
There you should be able to get the logs from your instance and find the actual error under /var/log/httpd/error_log.
You might also want to ssh to your instance and verify the paths are as you expect. You can of course also find the logs that way. If you are using the eb console tool, you can simply do eb ssh

Categories

Resources