So I recently started a Data Science course and learnt to use Amazon SageMaker Studio Lab (ASL) to create and run our DS projects. ASL is a free Machine Learning (ML) development environment that provides a web based virtual interface to perform all Data Science and Machine Learning steps. Its really easy to setup and use but I found one drawback for my use case. I wanted a Web Interface for my Data Science apps. So I wanted to test them locally and then deploy it to Heroku. Unfortunately ASL doesn't support browser in its virtual environment. So decided to set it up locally :)
In this blog I'll walk you thru the steps to setup dev environment for Data Science and Machine Learning locally. This is ideal for learning and quickly proto-typing ideas and applications, but not for training production Data Models as it might require a lot of processing power. We are going to use Docker and Visual Studio Code so setup the environment. We will also setup few VS Code plugins during the setup.
Install Docker by following the steps for respective platform here
Install Visual Studio Code by following the steps for respective platform here
In your workspace create a directory called Data Science. This will be the root directory for all Data Science related projects and applications.
Open the newly created Data Science directory in VS Code. Click Ctrl + Shift + P on Windows or Cmd + Shift + P in Mac to open the Command Pallet in VS Code
In the command pallet search for Remote-Containers and you should see a list of commands for Remote-Containers. Click on Remote Containers: Add Development Container Configuration Files


You should see the list of Dev Containers. These are various Docker dev environments that VSCode offers out of the box. From the list select Anaconda (Python 3).

For the Node version you can select lts or none depending on the use case.

DO NOT select any additional features to install and click ok. You should see a new folder called .devcontainer in the directory. Two files to look into are
devcontainer.json This file contains all the VSCode related options like settings, extensions etc. that we want when we run VSCode from Docker Container
Dockerfile This file builds the container and install all the required dependencies.

Next we need to build and open the folder in Docker Container. To do that, open the Command Pallet by clicking Ctrl + Shift + P in Windows or Cmd + Shift + P on Mac. Search for Remote-Containers and run Remote-Containers: Rebuild and Reopen in Container. This might take few mins depending on internet connection and machine, but luckily we need to do this only once.

Once the container is built, VS Code automatically maps the local directory to workspace directory in the container and reloads the IDE. You should see Dev Container: Anaconda (Python 3) in the lower left corner of VS Code. This means your folder structure is now opened in the container.

Confirm Installations by opening a terminal in VS Code from Command Pallet

Python version and running which python and python --version.
Conda installed and we can check by running conda --version command.
Ctrl + Shift + x on Windows or Cmd + Shift + x on Mac
Lets test the setup by creating a Jupyter Notebook. To do that,
Create new project directory called hello_world
Open a terminal in VS Code from Command Pallet and run the command to create new Jupyter Notebook
Create: New Jypyter Notebook
hello_world directory as destination.
VS Code command pallet run following command to select Conda interpreter for the notebook.Jupyter: Select Interpreter to Start Jupyter Server


hello_world.ipyb and insert following commands to insert install requirements. Installation should few mins depending on internet speed and machine.# ! conda install -c plotly plotly_express -y
# ! conda install pandas -y
# ! conda install numpy -y
# import the python libraries
import numpy as np
import pandas as pd
import plotly.graph_objs as go
import plotly.express as px
# define some variables
x_values=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
y_values=[15, 12, 8, 20, 19]
# create the data trace
trace = go.Bar(x=x_values, y=y_values)
# combine into a figure
fig = go.Figure([trace])
fig

Finally lets install Heroku CLI to create and deploy Heroku apps. Run the following command to install in VSCode Terminal
curl https://cli-assets.heroku.com/install.sh | sh;
Login into CLI by running.
heroku login -i
If you have 2FA turned on, for the password copy/paste the API Key from Heroku > Account Settings > API Key on the web portal.
Verify Heroku CLI installation. By running following command on VSCode Terminal,
heroku
We should see Heroku version and supported commands.

Now create a Python web application using Flask to quickly test our setup.
Open a terminal in VS Code and run following command to create virtual environment
python -m venv env
env directory inside hello_world
activate the environmentsource env/bin/activate

Flask librarypip install Flask

pip freeze > requirements.txt

app.py file in hello_world directory and add following Python code to create an hello world app.from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == "__main__":
app.run()
python app.py

VS Code will automatically forward port to our host machine,
Open in Browser or go to http://127.0.0.1:5000/ on your host machine to check our new Hello World web app locally.
Heroku we'll need to install gunicorn web server, by running following commandpip install gunicorn
requirements.txt by runningpip freeze > requirements.txt
Procfile to specify the commands executed by Heroku app on startup. More info can be found here. Copy paste the following contents into Procfileweb: gunicorn app:app
runtime.txt to define runtime environment for Heroku app. Add docker container python version in the runtimepython-3.9.12
git on this repo by running following commandsgit init
git add .
git commit -m "My first commit"
Heroku app by running following commandheroku create

Heroku app but also add new remote server for our git remote. Run the following command to confirm that,git remote -v

Herokugit push heroku master

Heroku CLI will build and deploy the app to the server, and can be accessed at the URL published in console logs

heroku logs --tail
So that was it, thats how I've setup my local environment using Docker and VS Code. Please feel free to comment with any suggestions, improvements or issues.
Happy Coding!