def root(): try: users = User.query.all() except Exception as e: DB.drop_all() DB.create_all() users = User.query.all() return render_template('base.html', title='Home', users=users)
def reset(): DB.drop_all() # Reset the DB DB.create_all() return render_template('base.html', title='Reset database!')
def setup_test_users(): DB.drop_all() DB.create_all() # initial setup add_test_users() return render_template('admin.html', message='test users added')
# to run in flask shell: exec(open("./add_users.py").read()) from twitoff.models import DB, User, Tweet DB.drop_all() DB.create_all() u1 = User(name='Mort') t1 = Tweet(text='What time is it?') t2 = Tweet(text='Are these my shoes?') t3 = Tweet(text='Not my chair, not my problem.') u1.tweets += [t1, t2, t3] u2 = User(name='Jack') t4 = Tweet(text='Rocket 88') t5 = Tweet(text='Deep Deep Sleep') t6 = Tweet(text="Somethin' Else") u2.tweets += [t4, t5, t6] adds = [u1, u2, t1, t2, t3, t4, t5, t6] for add in adds: DB.session.add(add) DB.session.commit()
def create_app(): """create and configure an instance of the application""" app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False DB.init_app(app) @app.route('/') def root(): return render_template('base.html', title='Home', users=User.query.all()) @app.route('/user', methods=['POST']) @app.route('/user/<name>', methods=['GET']) def user(name=None, message=''): name = name or request.values['user_name'] try: if request.method == 'POST': add_or_update_user(name) message = f'User {name} successfully added!' tweets = User.query.filter(User.name == name).one().tweets except Exception as e: message = f"Error adding {name}: {e}" tweets = [] return render_template('user.html', title=name, tweets=tweets, message=message) @app.route('/compare', methods=['POST']) def compare(message=''): user1, user2 = sorted( [request.values['user1'], request.values['user2']]) if user1 == user2: message = 'Cannot compare a user to themselves!' else: prediction = predict_user(user1, user2, request.values['tweet_text']) message = '"{}" is more likely to be said by {} than {}'.format( request.values['tweet_text'], user1 if prediction else user2, user2 if prediction else user1) return render_template('prediction.html', title='Prediction', message=message) @app.route('/update') def update(): # reset the database insert_example_user() return render_template('base.html', title='Users Updated!', users=User.query.all()) @app.route('/reset') def reset(): DB.drop_all() DB.create_all() return render_template('base.html', title='Reset database') @app.route('/trevor') def trevor(): hi = 'Hello Human, I am Trevor THE creator of TwitOff, well at least this version of twitoff...' \ 'designed by Lambda School, with a although by Aaron Gallant, and with lots of help from ' \ 'my TL Broken, and all my fellow students...' return hi return app
def reset(): DB.drop_all() DB.create_all() return redirect("/")
def create_app(): """ Creates and Configures a Flask application""" app = Flask(__name__) #app.config['SQLALCHEMY_DATABASE_URI'] = getenv('DATABASE_URI') app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///db.sqlite3" app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False DB.init_app(app) migrate.init_app(app, DB) @app.route('/') def root(): #new_user = User(id=1, name="nick", newest_tweet_id=1) #DB.session.add(new_user) #DB.session.commit() return render_template("base.html", title="Home", users=User.query.all()) @app.route('/compare', methods=['POST']) def compare(): user0, user1 = sorted( [request.values['user1'], request.values['user2']]) message = "" if user0 == user1: message = "Cannot compare users to themselves!" else: prediction = predict_user(user0, user1, request.values['tweet_text']) message = "{} is more likely to be said by {} than {}".format( request.values['tweet_text'], user1 if prediction else user0, user0 if prediction else user1) return render_template('prediction.html', title='Prediction', message=message) @app.route('/user', methods=['POST']) @app.route('/user/<name>', methods=['GET']) def user(name=None, message=""): name = name or request.values['user_name'] try: if request.method == 'POST': add_or_update_user(name) message = "User {} successfully added!".format(name) tweets = User.query.filter(User.name == name).one().tweets except Exception as e: message = "Error adding {}: {}".format(name, e) tweets = [] return render_template('user.html', title=name, tweets=tweets, message=message) @app.route('/update') def update(): # adds our users insert_example_users() return render_template('base.html', title="Home", users=User.query.all()) @app.route('/reset') def reset(): # resets database DB.drop_all() DB.create_all() return render_template('base.html', title='Home') return app
def reset(): # resets database DB.drop_all() DB.create_all() return render_template('base.html', title='Home')