comment pgadmin and create data_init

master
joseph 2 years ago
parent 645fb069fd
commit 412ffce2b4
  1. 10
      data_init/Dockerfile
  2. 1
      data_init/assets_tw.json
  3. 1
      data_init/assets_us.json
  4. 30
      data_init/data_init_tw_v0.py
  5. 29
      data_init/data_init_us_v0.py
  6. 15
      data_init/path.py
  7. 6
      data_init/requirements_data.txt
  8. 23
      data_init_tw_v0.py
  9. 22
      data_init_us_v0.py
  10. 48
      docker-compose.yml

@ -0,0 +1,10 @@
FROM python:3.9.6
WORKDIR /flask
ADD . /flask/data_init
RUN apt-get update
#RUN apt install nano
RUN pip install --upgrade pip
RUN pip3 install -r ./data_init/requirements_data.txt
CMD bash -c "python data_init/data_init_tw_v0.py && python data_init/data_init_us_v0.py"

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -0,0 +1,30 @@
import pandas as pd
import json
import yfinance as yf
import numpy as np
import psycopg2
from psycopg2.extras import execute_values
from tqdm import tqdm
with open('/flask/data_init/assets_tw.json') as f:
data_tw = json.load(f)
#SQL setting
SQL_CONFIG = dict(database="portfolio_platform", user='postgres', password='password', host='db',port ='5432')
# TW Stocks
conn = psycopg2.connect(**SQL_CONFIG)
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM stock_price_tw;")
row_count = cursor.fetchone()[0]
if row_count <= 0 :
for ticker in tqdm(data_tw):
df = yf.download(ticker, start="2013-1-1", progress=False)
value =[(ticker, df.index[i], df['Close'][i]) for i in range(len(df))]
with conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as curs:
sql = "insert into stock_price_tw (ticker, date, price) values %s"
execute_values(curs, sql, value)
print("Finish initialize")
else:
print("DB already have data")

@ -0,0 +1,29 @@
import pandas as pd
import json
import yfinance as yf
import numpy as np
import psycopg2
from psycopg2.extras import execute_values
from tqdm import tqdm
with open('/flask/data_init/assets_us.json') as f:
data_tw = json.load(f)
#SQL setting
SQL_CONFIG = dict(database="portfolio_platform", user='postgres', password='password', host='db',port ='5432')
# TW Stocks
conn = psycopg2.connect(**SQL_CONFIG)
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM stock_price;")
row_count = cursor.fetchone()[0]
if row_count <= 0 :
for ticker in tqdm(data_tw):
df = yf.download(ticker, start="2013-1-1", progress=False)
value =[(ticker, df.index[i], df['Close'][i]) for i in range(len(df))]
with conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as curs:
sql = "insert into stock_price (ticker, date, price) values %s"
execute_values(curs, sql, value)
print("Finish initialize")
else:
print("DB already have data")

@ -0,0 +1,15 @@
import os
import json
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_abspath = os.path.abspath(dir_path)
print("Abs Path : ",dir_abspath)
dir_list = os.listdir(dir_path)
print("Files and directories in '", dir_path, "' :")
# prints all files
print(dir_list)
with open('/flask/data_init/assets_us.json') as f:
data_tw = json.load(f)
print(data_tw)

@ -0,0 +1,6 @@
pandas==1.5.3
psycopg2==2.9.5
requests==2.28.2
SQLAlchemy==2.0.4
yfinance==0.2.22
tqdm==4.62.3

@ -1,23 +0,0 @@
import pandas as pd
import json
import yfinance as yf
import numpy as np
import psycopg2
from psycopg2.extras import execute_values
from tqdm import tqdm
with open('assets_tw.json') as f:
data_tw = json.load(f)
#SQL setting
SQL_CONFIG = dict(database="portfolio_platform", user='postgres', password='password', host='db',port ='5432')
# TW Stocks
conn = psycopg2.connect(**SQL_CONFIG)
for ticker in tqdm(data_tw):
df = yf.download(ticker, start="2020-1-1", progress=False)
value =[(ticker, df.index[i], df['Close'][i]) for i in range(len(df))]
with conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as curs:
sql = "insert into stock_price_tw (ticker, date, price) values %s"
execute_values(curs, sql, value)

@ -1,22 +0,0 @@
import pandas as pd
import json
import yfinance as yf
import numpy as np
import psycopg2
from psycopg2.extras import execute_values
from tqdm import tqdm
with open('assets_us.json') as f:
data_tw = json.load(f)
#SQL setting
SQL_CONFIG = dict(database="portfolio_platform", user='postgres', password='password', host='db',port ='5432')
# TW Stocks
conn = psycopg2.connect(**SQL_CONFIG)
for ticker in tqdm(data_tw):
df = yf.download(ticker, start="2020-1-1", progress=False)
value =[(ticker, df.index[i], df['Close'][i]) for i in range(len(df))]
with conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as curs:
sql = "insert into stock_price (ticker, date, price) values %s"
execute_values(curs, sql, value)

@ -14,21 +14,22 @@ services:
ports:
- "5432:5432"
networks:
- common_network
pgadmin:
image: dpage/pgadmin4:latest
environment:
PGADMIN_DEFAULT_EMAIL: jjjoey020629@gmail.com
PGADMIN_DEFAULT_PASSWORD: password
PGADMIN_LISTEN_PORT: 90
ports:
- "7070:90"
volumes:
- pgadmin-data:/var/lib/pgadmin
links:
- "db:pgsql-server"
networks:
- common_network
- common_network
#Comment for security
# pgadmin:
# image: dpage/pgadmin4:latest
# environment:
# PGADMIN_DEFAULT_EMAIL: jjjoey020629@gmail.com
# PGADMIN_DEFAULT_PASSWORD: password
# PGADMIN_LISTEN_PORT: 90
# ports:
# - "7070:90"
# volumes:
# - pgadmin-data:/var/lib/pgadmin
# links:
# - "db:pgsql-server"
# networks:
# - common_network
redis:
image: redis:7.0.11-alpine
container_name: myredis
@ -41,10 +42,9 @@ services:
flask:
build: ./
container_name: flask
command: bash -c "python data_init_tw_v0.py && python data_init_us_v0.py && python main.py runserver 0.0.0.0:8000"
command: bash -c "python main.py runserver 0.0.0.0:8000"
depends_on:
- db
- pgadmin
- redis
ports:
- 8000:8000
@ -53,8 +53,20 @@ services:
- 'redis'
networks:
- common_network
data_init:
build: ./data_init/
container_name: data_init
depends_on:
- db
- redis
- flask
links:
- 'db'
- 'redis'
networks:
- common_network
volumes:
db-data:
pgadmin-data:
# pgadmin-data:
networks:
common_network:

Loading…
Cancel
Save