Last active
August 29, 2015 14:00
-
-
Save zephraph/15c32a3335a8be21de05 to your computer and use it in GitHub Desktop.
Quick python script for setting up a new barebones flask workspace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
app = Flask(__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Credit goes to: https://www.digitalocean.com/community/articles/how-to-structure-large-flask-applications | |
# Statement for enabling the development environment | |
DEBUG = True | |
# Define the application directory | |
import os | |
BASE_DIR = os.path.abspath(os.path.dirname(__file__)) | |
# SQLite database for testing purposes | |
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(BASE_DIR, 'app.db') | |
DATABASE_CONNECT_OPTIONS = {} | |
# Application threads. A common general assumption is | |
# using 2 per available processor cores - to handle | |
# incoming requests using one and performing background | |
# operations using the other. | |
THREADS_PER_PAGE = 2 | |
# Enable protection agains *Cross-site Request Forgery (CSRF)* | |
CSRF_ENABLED = True | |
# Use a secure, unique and absolutely secret key for | |
# signing the data. | |
CSRF_SESSION_KEY = "secret" | |
# Secret key for signing cookies | |
SECRET_KEY = "secret" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from subprocess import call | |
from urllib2 import urlopen as get | |
import os, errno | |
def cmd(string): | |
call(string, shell=True) | |
def install(package): | |
if isinstance(package, basestring): | |
package = [package] | |
for p in package: | |
cmd("./env/bin/pip install %s" % p) | |
def download(url, dir=''): | |
downloaded = 0 | |
file_name = url.split('/')[-1] | |
if os.path.isfile(dir + file_name): | |
print file_name + " exists, skipping download" | |
return | |
request = get(url) | |
file = open(dir + file_name, 'wb') | |
while True: | |
buffer = request.read(4096) | |
if not buffer: | |
break | |
downloaded += len(buffer) | |
file.write(buffer) | |
print '\r%d bytes of %s downloaded' % (downloaded, file_name), | |
print ' Done!' | |
def mkdir(dir): | |
try: | |
os.makedirs(dir) | |
except OSError as e: | |
if e.errno == errno.EEXIST: | |
print "%s exsits, skipping creation" % dir | |
else: | |
raise | |
# Begin installation | |
cmd("virtualenv env") | |
install(["flask", | |
"flask-login", | |
"flask-openid", | |
"flask-mail", | |
"sqlalchemy", | |
"flask-sqlalchemy", | |
"sqlalchemy-migrate", | |
"flask-wtf", | |
"flask-bootstrap", | |
"flask-admin", | |
"flask-classy"]) | |
mkdir("app") | |
mkdir("app/templates") | |
mkdir("app/static") | |
download("https://gist.github.com/zephraph/15c32a3335a8be21de05/raw/config.py") | |
download("https://gist.github.com/zephraph/15c32a3335a8be21de05/raw/run.py") | |
download("https://gist.github.com/zephraph/15c32a3335a8be21de05/raw/__init__.py", 'app/') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!env/bin/python | |
# Start flask in debug mode | |
from app import app | |
app.run(host='0.0.0.0', port=8080, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment