Skip to content

Instantly share code, notes, and snippets.

@gcmurphy
Created July 5, 2013 06:55
Show Gist options
  • Select an option

  • Save gcmurphy/5932497 to your computer and use it in GitHub Desktop.

Select an option

Save gcmurphy/5932497 to your computer and use it in GitHub Desktop.
Automation of some boilerplate flask setup
#!/bin/bash
mkenv(){
virtualenv env
source env/bin/activate
pip install Flask \
Flask-WTF\
Flask-SQLAlchemy\
Flask-Classy\
Flask-Assets\
jsmin\
cssmin
}
mkconfig(){
cat > config.py <<EOF
import os
__base_dir = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/devel.db' % (__base_dir)
SECRET_KEY = "TODO: Replace this with something secure. Not suitable for deployment"
CSRF_ENABLED = True
CSRF_SESSION_KEY = "TODO: Replace this with something secure. Not suitable for deployment"
print("WARNING: Running insecure development configuration")
EOF
}
mkshell(){
cat > shell.py <<EOF
#!/usr/bin/env python
import os
import readline
from pprint import pprint
from flask import *
from app import *
os.environ['PYTHONINSPECT'] = 'True'
EOF
chmod +x shell.py
}
mkrun(){
cat > run.py <<EOF
#!/usr/bin/env python
from app import app
app.run(debug=True)
EOF
chmod +x run.py
}
mkapp(){
rootdir=$(pwd)
# Create basic application layout
mkdir app
mkdir app/static
mkdir app/templates
mkdir tmp
cd tmp
# The bootstraps
wget http://twitter.github.io/bootstrap/assets/bootstrap.zip
unzip -x bootstrap.zip
mv bootstrap/* ../app/static
cd $rootdir
rm -r ./tmp/
# The jQuery
cd $rootdir/app/static/js
wget http://code.jquery.com/jquery-2.0.3.min.js
# Create __init__.py
cd $rootdir/app
cat > __init__.py << EOF
from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.assets import Environment, Bundle
# Init app
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
# Register assets
assets = Environment(app)
javascripts = [
"js/jquery-2.0.3.min.js",
"js/bootstrap.min.js"
]
scripts = Bundle(*javascripts, filters='jsmin', output='site.js')
assets.register('javascripts', scripts)
stylesheets = [
"css/bootstrap.min.css"
]
styles = Bundle(*stylesheets, filters='cssmin', output='site.css')
assets.register('stylesheets', styles)
# TODO: INSERT BLUEPRINTS HERE
@app.route('/')
def index():
return "Hello"
EOF
cd $rootdir
}
mktemplates(){
rootdir=$(pwd)
cd $rootdir/app/templates
cat > base.html<<EOF
<!DOCTYPE html>
<html lang='en'>
<head>
<title>{% block title %}{% endblock%}</title>
<meta name="view-port" content="width=device-width, initial-scale=1.0">
{% block css %}
{% assets "stylesheets" %}
<link href="{{ASSET_URL}}" rel="stylesheet" media="screen">
{% endassets %}
{% endblock %}
</head>
<body>
<div id="header">
{% block header %}
{% endblock %}
</div>
<div id="message-wrap">
<div class="messages">
{% for category, msg in get_flashed_messages(with_categories=true) %}
{% endfor %}
</div>
</div>
<div class="container" id="content">
{% block content %}
{% endblock %}
</div>
<div id="footer">
{% block footer %}
{% endblock %}
</div>
{% block scripts %}
{% assets "javascripts" %}
<script type="text/javascript" src="{{ASSET_URL}}"></script>
{% endassets %}
{% endblock %}
</body>
</html>
EOF
mkdir forms
cd forms
cat > macros.html <<EOF
{% macro render_field(field) %}
<div class="control-group">
{{ field.label(class="control-label", for=field.short_name) }}
<div class="controls">
{{ field(id=field.short_name) }}
{% for error in field.errors %}
<span class="help-inline">[{{error}}]</span>
{% endfor %}
</div>
</div>
{% endmacro %}
EOF
cd $rootdir
}
mksite(){
mkenv
mkconfig
mkshell
mkrun
mkapp
mktemplates
}
mksite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment