Skip to content

Instantly share code, notes, and snippets.

@afro-coder
Created June 9, 2018 07:44
Show Gist options
  • Save afro-coder/2220cc0d2ae3e536fadfdea2b8a21a5f to your computer and use it in GitHub Desktop.
Save afro-coder/2220cc0d2ae3e536fadfdea2b8a21a5f to your computer and use it in GitHub Desktop.
Click using flask application factory (With different configurations)
"""
I'm really new to python and flask so this maybe terribly wrong but it works at least for me
usage:
python clitools.py --config {{your config}} command
It also prompts just in case you forget.
The ctx object from the click documentation along with the '@click.pass_context helps to push the app variable through
all the functions.
I've added more commands for checking if this works
Hope this helps
"""
import click
from feedback import create_app,db
class FlaskApp():
def __init__(self,x):
self.app=create_app(x)
@click.group()
@click.option('--config',default='development',prompt=True)
@click.pass_context
def cli(ctx,config):
ctx.obj=FlaskApp(config)
@cli.command('run_app',help="Run the flask app")
@click.pass_context
def run_app(ctx):
ctx.obj.app.run()
@cli.command('create_db',help="Creates the database")
@click.pass_context
def create_db(ctx):
with ctx.obj.app.app_context():
db.create_all()
@cli.command('drop_db',help="Drops the tables")
@click.pass_context
def drop_db(ctx):
if click.confirm("Are you sure"):
click.echo("Done")
with ctx.obj.app.app_context():
db.drop_all()
if __name__=="__main__":
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment