Automate Your Flask Development Workflow: Auto-Create a virtual environment, install all requirements, and run the Flask server without any fuss in Windows (PART 2)

Ashwin B
3 min readOct 19, 2023

This is the second part of the series “Automate your Flask development workflow”.

In PART 1 ( <- click here to check out part 1) of the series, I wrote a simple script to auto-activate a virtual environment and start the Flask development server. But In this part, let's make things more interesting.

Alright, imagine you and your friends are working on a Flask project, and not all of them are tech wizards. They’re curious and want to see how your backend and frontend come together or run the project on their laptops. I’ve got a nifty solution that makes it as easy as pie.

It’s like magic! 🎩✨ Here’s what the solution does:

  1. Virtual Env Time: First, it creates a cozy little space called a ‘virtual environment’ on their laptops. A virtual environment is like a separate workspace on a computer where software can be set up and used independently from the rest of the computer. It’s useful for keeping different projects or programs separate and organized, so they don’t interfere with each other.
  2. Requirements, Ahoy!: Next, it installs all the necessary ingredients (we call them ‘requirements’) for your project.
  3. Flask Showtime: Finally, it waves its wand and starts your Flask server. It’s like a magician saying, “Abracadabra,” and voila! Your project is running, ready for your friends to explore.

The nifty solution:

@echo off

echo Creating a virtual environment...
python -m venv venv

if errorlevel 1 (
echo Failed to create a virtual environment.
exit /b 1
)

echo Virtual environment created successfully.

echo Activating virtual environment...
call venv\Scripts\activate

if errorlevel 1 (
echo Failed to activate virtual environment.
exit /b 1
)

echo Virtual environment activated successfully.

echo Installing requirements...
pip install -r requirements.txt

if errorlevel 1 (
echo Failed to install requirements.
exit /b 1
)

echo Requirements installed successfully.

echo Running Flask application...
python app.py

if errorlevel 1 (
echo Failed to run Flask application.
exit /b 1
)

echo Flask application is running.

The output:

Save this file with the extension of “.bat”, and add this along with the flask project, ensure this is placed in the same folder as the main flask application. When your friends want to run the project, they can just click the “.bat” file, making it super easy. No need to explain complex commands or use advanced tools like Docker. It’s like magic for running the project!

Note: you can make changes to the script as per your requirements.

Some people might wonder, if this script creates a virtual environment, installs the requirements, and runs the flask application every time there is no point right, Well, here’s the answer for this: I’ve got two scripts to keep things super easy. One script sets up your virtual environment, installs everything, and runs the server (part 2, which is this one), while the other script just activates the existing virtual environment and fires up your server(part 1). By doing this, I hope I’ve made life a breeze for anyone, even those with zero development experience or college students looking to impress their classmates with a sleek demo. Cheers!

--

--

Ashwin B

I love the internet, technology and building beautiful things