Skip to content

Instantly share code, notes, and snippets.

@uolter
Last active October 12, 2015 15:18
Show Gist options
  • Save uolter/4046732 to your computer and use it in GitHub Desktop.
Save uolter/4046732 to your computer and use it in GitHub Desktop.
flask application basic setup (python flaskapp_init.py -a <myapp> )
#!/usr/local/bin/python
from optparse import OptionParser
import os
PATH_SEPARATOR = os.sep
html_layout = "<!DOCTYPE html>\n" \
"<html>\n" \
" <head>\n" \
" <title>%s App</title>\n" \
" </head>\n" \
" <body>\n" \
" <div class='container'>\n" \
" <div class='page-header'>\n" \
" <h1>Flask %s App</h1>\n" \
" </div>\n" \
" {%% block content %%}\n" \
"\n" \
" {%% endblock %%}\n" \
" <hr/>\n" \
" <div class='modal-footer'>\n" \
" <h4>&copy; pypix</h4>\n" \
" </div>\n" \
" </div>\n" \
"</body>\n" \
"</html>"
html_page = "{% extends 'layout.html' %}\n" \
"{% block content %}\n" \
"<p>Your code goes here!!\n" \
"{% endblock %}"
view_script = "from app import app\n" \
"from flask import render_template\n\n" \
"@app.route('/')\n" \
"def index():\n" \
" return render_template('index.html')\n\n" \
"# your code goes here."
run_server_script = "from app import app\n" \
"\n" \
"app.run(debug=True)"
init_script = "from flask import Flask\n" \
"\n" \
"app = Flask(__name__)\n\n" \
"\n" \
"from app import views"
def createPath(path):
if not os.path.isdir(path):
os.makedirs(path)
def create_runserver(appname):
run_server = PATH_SEPARATOR.join([appname, 'run.py'])
if not os.path.exists(run_server):
script = open(run_server, 'w')
script.write(run_server_script)
script.close()
def create_init(appname):
init_file = PATH_SEPARATOR.join([appname, 'app', '__init__.py'])
if not os.path.exists(init_file):
script = open(init_file, 'w')
script.write(init_script)
script.close()
def create_view(appname):
view_file = PATH_SEPARATOR.join([appname, 'app', 'views.py'])
if not os.path.exists(view_file):
script = open(view_file, 'w')
script.write(view_script )
script.close()
def init(appname):
# create the application root
createPath(PATH_SEPARATOR.join([appname, 'app']))
# __init__
create_init(appname)
# create run server script
create_runserver(appname)
# create view
create_view(appname)
# create the static directory
path = [appname, 'app', 'static']
createPath(PATH_SEPARATOR.join(path))
# css directory
path = [appname, 'app', 'static', 'css']
createPath(PATH_SEPARATOR.join(path))
# js directory
path = [appname, 'app', 'static', 'js']
createPath(PATH_SEPARATOR.join(path))
# create the image static directory
path = [appname, 'app', 'static', 'img']
createPath(PATH_SEPARATOR.join(path))
# create the template directory
path = [appname, 'app', 'templates']
createPath(PATH_SEPARATOR.join(path))
# create the index.html
index = PATH_SEPARATOR.join([appname, 'app', 'templates', 'index.html'])
if not os.path.exists(index):
index_file = open(index, 'w')
index_file.write(html_page)
index_file.close()
# create the layout.html
layout = PATH_SEPARATOR.join([appname, 'app', 'templates', 'layout.html'])
if not os.path.exists(layout):
layout_file = open(layout, 'w')
layout_file.write(html_layout % (appname, appname))
layout_file.close()
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-a", "--app", dest="appname",
help="application name", default=None)
(options, args) = parser.parse_args()
if options.appname:
print 'Start ...'
print ' creating %s app' %options.appname
init(options.appname)
print ' done !!!'
else:
print ' application name required!! Type -h for the help.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment