Python local development environment construction template [Flask / Django / Jupyter with Docker + VS Code]

Overview

Building a local environment comes with some costs. Of course, I really understand the opinion that you should have some trouble here. However, I think it's a waste to lose my feelings. Therefore, I created a template for Flask / Django / Jupyter Notebook that allows you to complete ** Python development environment construction with 3 commands **. (Excluding cd)

Prerequisites

--Being MacOS --The git and make commands are valid (should be valid by default)

Local build image

You will run the app on the Docker container. Even if you're not familiar with it, you can say that you're developing with a Docker container **.

Flask_Nginx_unicorn_diagramdrawio-local-template (1).png

--The directory is provided from the host PC to the container by bind mount. This makes it possible to read the contents of the file edited on the host PC side on the container side. --For access from the host PC, port forwarding is adopted. The connection port on the container side has been changed depending on the project, but all the ports on the host side to forward have been unified to 8000.

Construction procedure

1. Download template

Describes how to download with git clone.

Terminal


git clone [email protected]:mintak21/local-py-dev-template.git

2. Extract the template to the development directory

Specify the extraction destination directory and extract the project. Use the make command properly according to the created project.

Terminal


cd local-py-dev-template
#When creating a jupyter project
make create_jupyter_pj
#When creating a flask project
make create_flask_pj
#When creating a django project
make create_django_pj

--Specifiable arguments

Argument name Overview Default value
TARGET_DIR Extraction directory(Create if it does not exist) ~/workspace/project
DJANGO_PJ_NAME Can be specified for django. Django template project name mysite

Command example with arguments

Terminal


#One level higher flask_Extract the flask project to the dir directory
make create_flask_pj TARGET_DIR=../flask_dir
# ~/Expand the django project in the workspace directory and name the django project blog
make create_django_pj TARGET_DIR=~/workspace DJANGO_PJ_NAME=blog

3. Start container

Move to the directory extracted in 2, create a Docker image and container, and start it.

Terminal


cd ${TARGET_DIR}
make run

4. Connection

Since both containers are port forwarding on port 8000 of the host PC, you can connect if you can access the following, and if the contents described are displayed, the setting is completed.

Access: http: // localhost: 8000 /

project Expected results
Jupyter On the Jupyter home screen,HelloPython.ipynbThe file is visible
Flask Hello Flask!Is displayed
Django Rocket is out

5. Shut down container

Since it is running in Foreground, stopping the process on the terminal will stop the container.

Ctrl + C in the terminal

Bonus. Visual-Studio-Code setup

At this point, we have started and connected, so we will proceed with development from now on. There are also development editors such as Vim and pytorch, but here we are using Visual-Studio-Code to proceed with development. Installation is completed with one command.

Terminal


make setup_vscode # local-py-dev-Run in template directory

Setting outline

Not only installing Visual-Studio-Code, but also setting static analysis and pytest, and introducing necessary & useful extensions at the same time.

  1. Install Visual-Studio-Code with brew cask
  2. Package introduction (pip) --Static analysis: flake8 --import alignment: isort --Formatter: autopep8 --type check: mypy --Test: pytest
  3. Extension installation (code --install-extension) --Microsoft-Python: Python extension tool (required) --Bracket Pair Colorizer: Corresponding brackets are easier to see in color --Whitespace +: Visualize tabs and whitespace --Docker: Docker can be handled with vscode --vscode-icons: Make icons easier to see --autoDocString: DocString creation assistance

Others The local side should also use a virtual environment such as pipenv, but since many of the pythonic seniors I knew were using pure Python, I introduced a virtual environment following this. Is not ...

Reference Github repository

Recommended Posts

Python local development environment construction template [Flask / Django / Jupyter with Docker + VS Code]
Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
Create a simple Python development environment with VS Code and Docker
[Python] Build a Django development environment with Docker
Easy Python data analysis environment construction with Windows10 Pro x VS Code x Docker
Application development with Docker + Python + Flask
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
Build Jupyter Lab (Python) environment with Docker
[No venv required] The strongest Python development environment created with Remote Containers [VS Code / Docker]
How to build Python and Jupyter execution environment with VS Code
VS Code + Azure Functions + Python environment construction procedure
Build Django + NGINX + PostgreSQL development environment with Docker
Analytical environment construction with Docker (jupyter notebook + PostgreSQL)
Python (Windows 10) Virtual Environment / Package with VS Code
Create Nginx + uWSGI + Python (Django) environment with docker
Use Python in Anaconda environment with VS Code
Use Docker development container conveniently with VS Code
Make your Python environment "easy" with VS Code
Build a python execution environment with VS Code
Python development environment construction
python2.7 development environment construction
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
[Python] OpenCV environment construction with Docker (cv2.imshow () also works)
Allow real-time code checking in Python development with VS Code
Web application made with Python3.4 + Django (Part.1 Environment construction)
Python development environment with Windows + Anaconda3 + Visual Studio Code
QGIS3 Python plugin development environment construction with VSCode (macOS)
Python development environment with Windows + Python + PipEnv + Visual Studio Code
Remote debug Django environment created with docker-compose with VS Code
Build a development environment with Poetry Django Docker Pycharm
Python with VS Code (Windows 10)
Debug Python with VS Code
Docker + Django + React environment construction
Prepare python3 environment with Docker
[MEMO] [Development environment construction] Python
[Django] Use VS Code + Remote Containers to quickly build a Django container (Docker) development environment.
Environment construction for those who want to study python easily with VS Code (for Mac)
django project development environment construction
Let's run jupyter natively supported by VS Code with python3.8
Set up a Python development environment with Visual Studio Code
[Memo] Build a development environment for Django + Nuxt.js with Docker
Construction of Python local development environment Part 2 (pyenv-virtualenv, pip usage)
Golang + Gin + Docker hot reload, VS Code debugging environment construction
Create a VS Code + Docker development environment on a Linux VM
Prepare Python development environment with Mac + Windows + VisualStudio Code (Windows version)
From environment construction to deployment for flask + Heroku with Docker
[Django] Build a Django container (Docker) development environment quickly with PyCharm
Prepare Python development environment with Mac + Windows + VisualStudio Code (Mac version)
Create a simple Python development environment with VSCode & Docker Desktop
UpNext2 Development Record # 1 Build Python CI environment in VS Code
Create a Python (Django) learning environment with Docker so that you can debug with VS Code (almost your own procedure memo)
Get started with Python! ~ ① Environment construction ~
[For beginners] Django -Development environment construction-
Easy Jupyter environment construction with Cloud9
[Python3] Development environment construction << Windows edition >>
Python development environment construction on macOS
[MEMO] [Development environment construction] Jupyter Notebook
Try running Jupyter with VS Code
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
Emacs Python development environment construction memo