Skip to content

Instantly share code, notes, and snippets.

@franz-net
Created December 8, 2021 09:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franz-net/147f7513ab6aee450c89373bb5ba988d to your computer and use it in GitHub Desktop.
Save franz-net/147f7513ab6aee450c89373bb5ba988d to your computer and use it in GitHub Desktop.
Bash script to init a Flask App.

Flask_init

What does it do?

  • Creates a folder called as the argument app_name in the path provided
  • Creates a python virtual environment in that path
  • Installs Flask in the virtual env
  • Creates a boilerplate application
  • Creates gitignore
  • Creates a bootstrap script that automatically activates the virtual environment and runs the app when executed
  • Can also create a requirements.txt file if the parameter --package is passed

Requirements:

  • Python3

Installation:

  • Download the file to your linux computer (might work for mac)
  • Add execute permissions by running chmod +x flask_init.sh

Run app

./flask_init.sh --app_name myApp --path /opt/flask_examples ./flask_init.sh --app_name myApp --path /opt/flask_examples --package

#! /bin/bash
# Created by franz
set -o errexit
#set -x
# DEFAULTS
package="false"
# HELPERS
function throw_if_empty() {
local name="$1"
local value="$2"
if [ -z "$value" ]; then
echo "Parameter '$name' cannot be empty." 1>&2
printUsage
exit -1
fi
}
function checkRequirements() {
if ! command -v python3 >/dev/null; then
echo "Error! Could not find python3" 1>&2
printUsage
exit -1
fi
}
# FUNCTIONS
function createRequirementFile() {
pushd "$FULL_PATH"
source env/bin/activate
pip freeze > requirements.txt
deactivate
popd
}
function createApp(){
mkdir -p "$FULL_PATH"
pushd "$FULL_PATH"
mkdir static templates
createEnv
createGitIgnore
initApp
createBootstrapScript
popd
}
function createBootstrapScript() {
cat <<EOF > bootstrap.sh
#!/bin/bash
export FLASK_APP=./app.py
export FLASK_DEBUG=1
export FLASK_RUN_PORT=8080
source env/bin/activate
flask run
EOF
}
function initApp() {
cat <<EOF > app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "<h1>Hello World</h1>"
#app.run(debug=True)
EOF
}
function createEnv() {
python3 -m venv env
source env/bin/activate
pip install flask
}
function createGitIgnore() {
cat <<EOF > .gitignore
env
__pycache__
EOF
}
# USAGE
echo $@
function printUsage() {
cat <<EOF
Will create a flask app boilerplate with a pyenv and Flask installed
It will also create a base app with '/' route and a static and
templates directories along with gitignore
Command
$0
Requirements:
* Python 3
* python3.9-venv (if running on debian based linux)
Arguments:
--app_name : Required, Flask folder name
--path : Path where the folder will be created
--package : Creates a requirements.txt in the path provided
Example:
./"$0" --app_name myApp --path /opt/flask_examples
./"$0" --app_name myApp --path /opt/flask_examples --package
EOF
}
while [[ $# >0 ]]
do
key="$1"
shift
case $key in
--app_name)
app_name="$1"
shift
;;
--path)
app_path="$1"
shift
;;
--package)
package="true"
;;
--help|-help|-h)
printUsage
exit 13
;;
*)
echo "Error! Unknown argument '$key to script '$0'" 1>&2
printUsage
exit -1
esac
done
throw_if_empty --app_name "$app_name"
throw_if_empty --path "$app_path"
checkRequirements
FULL_PATH="$app_path"/"$app_name"
if [ "$package" = "true" ]; then
createRequirementFile
fi
if [ "$package" = "false" ]; then
createApp
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment