Skip to content

RickZhou666/python-flask-vsc

Repository files navigation

Python Setup

1. Tips

1.1 with open mode

# https://codeahoy.com/learn/tutorials/python-open/
Mode   Description	                          File Pointer Position	      Creates File if Not Exists	      Truncates Existing File
r      Read-only	                            Beginning of the file	      No	                              No
r+	   Read and write (updating)	            Beginning of the file	      No	                              No
w	     Write-only (overwrite or create)	      Beginning of the file	      Yes	                              Yes
w+	   Write and read (overwrite or create)	  Beginning of the file	      Yes	                              Yes
a	     Append-only (append or create)	        End of the file	            Yes	                              No
a+	   Append and read (append or create)	    End of the file	            Yes	                               No

python-flask-vsc

1. Docker Command

install dependencies

$ pip install -r requirements.txt
$ pip --default-timeout=2000 install -r requirements.txt
$ pip install --no-cache-dir -r requirements.txt

$ pip uninstall -r requirements.txt -y

# if met subprocess error, try to install a lower version of dep first
$ pip install mysqlclient=2.1.1
$ pip install -r requirements.txt

build image

$ docker build -t rest-apis-flask-python .

run image in a container

  1. use -v "$(pwd):/app" otherwise code changes is not reflected in docker container
$ docker run -dp 5000:5000 -w /app -v "$(pwd):/app" --name flask-smorest-api-rick rest-apis-flask-python

# sh -c "flask run" 
# above cmd line will tell server not using CMD line from Dockerfile but use flask run to start the server
$ docker run -dp 5000:5000 -w /app -v "$(pwd):/app" --name flask-smorest-api-rick rest-apis-flask-python sh -c "flask run"

display all images

$ docker images -a

remove multiple images

$ docker image rm 8ae668a96ce6 aa4ecf532269 72d52a866646 2fa08ed06206 6840131c5619 7fb60d0ea30e 157095baba98

display all container

$ docker ps -a 

not able to use autocomplete with terminal inside docker container

Ref try to login from terminal

$ docker exec -it <container-id> bash
$ docker exec -it f36a9e11f47e bash




##########################

SETUP virtualenv

##########################

  1. create virtualenv
$ pyenv virtualenv 3.10.8 flask-vsc-3.10.8
  1. list existing virtualenvs $ pyenv virtualenvs ➜ python-flask-vsc git:(develop) ✗ pyenv virtualenvs 3.10.8/envs/flask-vsc-3.10.8 (created from /Users/runzhou/.pyenv/versions/3.10.8) flask-vsc-3.10.8 (created from /Users/runzhou/.pyenv/versions/3.10.8)

There are two entries for each virtualenv, and the shorter one is just a symlink.

  1. activate your virtualenv
$ pyenv activate flask-vsc-3.10.8
$ pyenv deactivate
  1. update pip
$ python3.10 -m pip install --upgrade pip
  1. delete virtualenv
$ pyenv uninstall my-virtual-env

https://github.com/pyenv/pyenv-virtualenv




##########################
SETUP local venv
##########################

  1. create local virtual env $ python3.10 -m venv .venv

  2. select interpreter cmd + shift + P

  3. activate your virutal env $ source .venv/bin/activate




##########################
check flask
########################## $ which flask (.venv) ➜ python-flask-vsc git:(develop) ✗ which flask /Users/runzhou/git/python-flask-vsc/.venv/bin/flask

2. Deploy on gcp

  1. ssh to your gcp machine
$ ssh 10.176.22.192
  1. go to your home folder and create github pub key
$ cd /x/home/runzhou

$  ssh-keygen -t rsa -b 4096 -C "runzhou666@gmail.com"
  1. clone repo to local
git clone git@github.com:RickZhou666/python-flask-vsc.git
  1. check port avaialble to avoid conflict
sudo lsof -i -P -n | grep LISTEN
  1. build docker image local
sudo docker build -t python-flask-vsc .
  1. run in container
sudo docker run -dp 5000:5000 -w /app -v "$(pwd):/app" --restart=always --name python-flask-vsc_container python-flask-vsc

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published