Skip to content

Commit

Permalink
Use Flask-Script for run script.
Browse files Browse the repository at this point in the history
  • Loading branch information
colekettler committed Oct 1, 2015
1 parent f8ada2f commit a6aea74
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 26 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,16 @@ export MY_COOL_API_DEVELOPMENT_DATABASE_URI=postgres://localhost/mydb

(Prefixed with your app's name, for your convenience.)

Start up Flask's local server:
If you're using SQLAlchemy, start up a Python shell and initialize your database:

```
./run.py
echo 'db.create_all()' | ./manage.py shell
```

Start up Flask's local development server:

```
./manage.py runserver
```

Enjoy! But hurry up and add some tasty [resources](#resource) to consume:
Expand Down Expand Up @@ -172,6 +178,15 @@ yo flask-api:version

## Notes

### Manager Script

[Flask-Script](https://flask-script.readthedocs.org/en/latest/) is used to create an executable Python script to handle common tasks. Out of the box, you can:

* Start up Flask's development server with `./manage.py runserver`.
* Start a Python shell with a predefined context with `./manage.py shell`. Your created `app` instance will be made available, along with your `db` instance if you're using SQLAlchemy (run `db.create_all()` to initialize your database).

Check out the docs for more info and how to add more commands.

### Python Support

The boilerplate is pretty simple, and all of the default dependencies support Python 2 and 3. It should Just Work™.
Expand Down
13 changes: 7 additions & 6 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,12 @@ module.exports = AllYourBase.extend({

runnable: function () {
this.fs.copyTpl(
this.templatePath('run.py'),
this.destinationPath('run.py'),
this.templatePath('manage.py'),
this.destinationPath('manage.py'),
{
appName: this.appName,
appEnvVar: this.appName.toUpperCase()
appEnvVar: this.appName.toUpperCase(),
databaseMapper: this.config.get('databaseMapper')
}
);
},
Expand Down Expand Up @@ -315,7 +316,7 @@ module.exports = AllYourBase.extend({
if (!this.options['skip-install']) {
this.log(chalk.cyan('Installing dependencies...'));

python.pipInstall('flask-marshmallow');
python.pipInstall('flask-marshmallow', 'flask-script');

if (this.config.get('database') === 'postgresql') {
python.pipInstall('psycopg2');
Expand Down Expand Up @@ -349,9 +350,9 @@ module.exports = AllYourBase.extend({
this.log(chalk.green('\nAll set!\n'));

this.log(chalk.cyan(
'You can run Flask\'s local server by executing the run script:'
'You can run Flask\'s local server by executing the manager script:'
));
this.log(chalk.bold('./run.py\n'));
this.log(chalk.bold('./manage.py runserver\n'));

this.log(chalk.cyan(
'For safety\'s sake, this defaults to a production config with DEBUG ' +
Expand Down
20 changes: 20 additions & 0 deletions generators/app/templates/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#! /usr/bin/env python

import os

from flask.ext.script import Manager

from <%= appName %> import create_app<% if (databaseMapper === 'sqlalchemy') { -%>, db<% } %>


app = create_app(os.getenv('<%= appEnvVar %>_CONFIG', 'default'))
manager = Manager(app)


@manager.shell
def make_shell_context():
return dict(app=app<% if (databaseMapper === 'sqlalchemy') { -%>, db=db<% } %>)


if __name__ == '__main__':
manager.run()
12 changes: 0 additions & 12 deletions generators/app/templates/run.py

This file was deleted.

12 changes: 6 additions & 6 deletions test/test-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('app', function () {
'.editorconfig',
'.yo-rc.json',
'config.py',
'run.py'
'manage.py'
]);
});

Expand All @@ -74,8 +74,8 @@ describe('app', function () {

it('creates an executable run script', function () {
assert.fileContent([
['run.py', /app\.run\(\)/],
['run.py', /#! \/usr\/bin\/env python/]
['manage.py', /manager\.run\(\)/],
['manage.py', /#! \/usr\/bin\/env python/]
]);
});

Expand All @@ -94,7 +94,7 @@ describe('app', function () {
it('defaults to production config', function () {
assert.fileContent([
['config.py', /'default': ProductionConfig/],
['run.py', /default/]
['manage.py', /default/]
]);
});

Expand Down Expand Up @@ -145,11 +145,11 @@ describe('app with name', function () {
});

it('correctly references the app directory in the run script', function () {
assert.fileContent('run.py', /from karate import/);
assert.fileContent('manage.py', /from karate import/);
});

it('namespaces the app config environment variable', function () {
assert.fileContent('run.py', /os\.getenv\('KARATE_CONFIG'/);
assert.fileContent('manage.py', /os\.getenv\('KARATE_CONFIG'/);
});
});

Expand Down

0 comments on commit a6aea74

Please sign in to comment.