Last active
June 23, 2020 14:36
-
-
Save jpurrutia/c9f8a92f6f2167d9a1e0b14c5614b341 to your computer and use it in GitHub Desktop.
Flask App Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, render_template, redirect, url_for | |
from form import CoefficientsForm | |
from utils import calculate_roots, save_to_db | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = '123' | |
@app.route('/', methods=['GET', 'POST']) | |
def calculate(): | |
form = CoefficientsForm() | |
if form.validate_on_submit(): | |
return redirect(url_for('result', a=form.data['a'], b=form.data['b'], c=form.data['c'])) | |
return render_template('form.html', form=form) | |
@app.route('/result') | |
def result(): | |
save_to_db(request=request) | |
return calculate_roots(a=request.args['a'], b=request.args['b'], c=request.args['c']) | |
if __name__ == '__main__': | |
app.debug = True | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a>Solve <img src="http://latex.codecogs.com/svg.latex?ax^2+bx+c=0" border="0"/></a> | |
<br><br> | |
<form action="{{ url_for('calculate') }}" method="post"> | |
<a>a=</a> | |
<input type="text" name="a"> | |
<a>b=</a> | |
<input type="text" name="b"> | |
<a>c=</a> | |
<input type="text" name="c"> | |
{{ form.csrf_token }} | |
<input type="submit"> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymysql.cursors | |
import rds_config | |
def calculate_roots(a, b, c): | |
""" | |
Calculates roots of quadratic equation of form `ax^2 + bx + c = 0` | |
:param a: coefficient of x^2 | |
:param b: coefficient of x | |
:param c: constant | |
:return: roots if they exist | |
""" | |
a, b, c = map(float, (a, b, c)) | |
if a == 0: | |
return str(-c / b) | |
discriminant = b**2 - 4 * a * c | |
if discriminant < 0: | |
return 'No real roots' | |
x1 = (-b + discriminant ** 0.5) / (2 * a) | |
x2 = (-b - discriminant ** 0.5) / (2 * a) | |
if x1 == x2: | |
return str(x1) | |
return '{},{}'.format(x1, x2) | |
def save_to_db(request): | |
db = pymysql.connect(host=rds_config.db_endpoint, | |
user=rds_config.db_username, | |
passwd=rds_config.db_password, | |
port=rds_config.db_port) | |
cursor = db.cursor() | |
cursor.execute('INSERT INTO mydb.inputs (a,b,c) VALUES (%s, %s, %s);' ,(request.args['a'], request.args['b'], request.args['c'])) | |
db.commit() | |
db.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment