DEV Community

KenjiGoh
KenjiGoh

Posted on

Shell script to start up a flask project in a venv

Copy & Save it as flask_starter.sh

#!/bin/bash

# Inform the user about the script purpose
echo "This script will set up a Flask project."

# Ask for project directory name
read -p "Enter the name of your Flask project: " project_name

# Inform the user about creating the project directory
echo "Creating project directory: $project_name"
# Create project directory
mkdir "$project_name"
cd "$project_name"

# Inform the user about setting up the virtual environment
echo "Setting up virtual environment..."
# Set up virtual environment
python -m venv venv

# Inform the user about activating the virtual environment
echo "Activating virtual environment..."
# Activate virtual environment
source venv/Scripts/activate

# Inform the user about installing Flask
echo "Installing Flask..."
# Install Flask
pip install Flask

# Inform the user about creating app.py
echo "Creating app.py..."
# Create app.py
cat <<EOF > app.py
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)
EOF

# Inform the user about creating templates directory
echo "Creating templates directory..."
# Create templates directory
mkdir templates

# Inform the user about creating index.html template
echo "Creating index.html template..."
# Create index.html template
cat <<EOF > templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flask App</title>
</head>
<body>
    <h1>Hello, Flask!</h1>
</body>
</html>
EOF

# Inform the user that setup is complete
echo "Setup completed successfully!"

# Open the project in Visual Studio Code
echo "Opening the project in Visual Studio Code..."
code .

# Run the Flask app
echo "Running the Flask app..."
python app.py
Enter fullscreen mode Exit fullscreen mode

Command to run the script

bash flask_starter.sh
Enter fullscreen mode Exit fullscreen mode

Now go to browser to see the app

http://localhost:5000/
Enter fullscreen mode Exit fullscreen mode

Top comments (0)