Last active
December 4, 2018 03:10
-
-
Save shimo164/0caa23bfd1fb2b089839bb950b0516ab to your computer and use it in GitHub Desktop.
Simple web app
This file contains hidden or 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 os | |
| import sqlite3 | |
| from flask import Blueprint, Flask, current_app, g, render_template, request | |
| # manually set instance dir | |
| app = Flask(__name__, instance_path='/home/../simple/instance') | |
| app.config.from_mapping( | |
| DATABASE=os.path.join(app.instance_path, 'simple.sqlite') | |
| ) | |
| def get_db(): | |
| if 'db' not in g: | |
| g.db = sqlite3.connect( | |
| current_app.config['DATABASE'], | |
| detect_types=sqlite3.PARSE_DECLTYPES | |
| ) | |
| g.db.row_factory = sqlite3.Row | |
| return g.db | |
| def close_db(e=None): | |
| db = g.pop('db', None) | |
| if db is not None: | |
| db.close() | |
| def RepresentsInt(s): | |
| try: | |
| int(s) | |
| return True | |
| except ValueError: | |
| return False | |
| @app.route('/', methods=('GET', 'POST')) | |
| def hello(): | |
| if request.method == 'POST': | |
| enter_value = request.form['value'] | |
| if RepresentsInt(enter_value): | |
| db = get_db() | |
| error = None | |
| if error is None: | |
| db.execute( | |
| 'INSERT INTO post (value) VALUES (?)', (enter_value,) | |
| ) | |
| db.commit() | |
| return render_template('index.html') | |
| else: | |
| return 'enter int' | |
| flash(error) | |
| return render_template('index.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment