The main purpose of this repo is to create a sample Python app and try making a consistent workflow locally for development. Visual Studio Code can do it so easily with just 1 plugin Remote Development with Docker containers. The steps can be altered for whatever the chosen environment required is. This also possible with WSL.
Motivation: I would like to develop and run my Python code remotely/in a virtualized environment (similar to prod) without installing Python or any dev related packages/utils on my Windows machine. Instead, I just want to spin up a Python Docker container where I can find everything inside what I need.
Main tasks I want to achieve:
- Setup remote Python development with Visual Studio Code
- Create some simple REST APIs with Flask to
- return static index.html at
"/"
- return text "Hello World" under
"/hello"
- return Top 250 movies from IMDb at
"/imdb/movies/top250"
- scrape Imdb Top 250 movies table from webpage with using beautifulsoup4,
- parse into a class in "/model/topMovie.py"
- serialize objects and return as Json array
{ "results": [ { "rank": "1.", "rating": "9.2", "title": "Die Verurteilten" }, ... }
- return static index.html at
The Visual Studio Code Remote - Containers extension lets you use a Docker container as a full-featured development environment. VS Code Remote guide
With the help of these plugins you can easily create and develop your Python code.
- In the root directory of your project, you need to create a folder called .devcontainer
- This is where we will store the settings for our environment.
- The naming is important!
- Under this .devcontainer folder:
- Create a devcontainer.json file (Hit F1 and select 'Remote-containers Create container configuration files') Basically a config file that determines how your dev container gets built and started.
- Create a docker-compose.yml Sample With this we can setup more complex environments and add additional services like databases etc.
- Create a Dockerfile Sample Within we choose the image and we can add any RUN commands (such as global installs).
- You can create a 'requirements.txt' or 'requirements.temp.txt' to define which Python dependencies needed to be installed for your app (E.g.: flask)
- For install requirements run this command inside the devcontainer:
pip install -r requirements.txt
- For install requirements run this command inside the devcontainer:
- Add some sample python code (app.py and static files) from VS code try python
- Hit F1 and select 'Remote-Containers: Reopen Folder in Container'
- This will reopen VS Code and your workspace in a remote window, connected to the VS Code server which is running inside the container based on your Dockerfile.
- Once the container is running and you're connected, you should see your remote context change in the bottom left of the Status bar:
- Check your environment
python --version
andpip list
- For start the Python app we simply should execute this command from the VS Code terminal: 'flask run'
- For make the APIs available from the host we first need to forward the ports from the container.
- For this Hit F1 and select 'Forward a Port'
- In older versions 'Remote-Containers: Forward Port from Container'
- Enter port number 5000.
- For this Hit F1 and select 'Forward a Port'
- Open your browser and navigate to
localhost:5000
- (In older versions Click "Open Browser" in the notification that appears to access the web app on this new port.)
Notes:
- Files added/created at the workspace in the host outside the devcontainer context not gonna appear. You need to rebuild the devcontainer. Hit F1 then select option "Remote-Containers: Rebuild Container"