Python

django-web on GCE

개요 django-web을 GCE에 설치 및 배포를 간단하게 진행하도록 한다. 사전준비 Google Cloud Platform 회원가입은 미리 진행했고, GCE 인스턴스를 생성할 줄 아는 상태임을 전제로 한다. Miniconda 설치가 끝난 상황임을 가정한다. Miniconda Linux 설치 : https://docs.anaconda.com/free/miniconda/ Miniconda 설치 mkdir -p ~/miniconda3 wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3 rm -rf ~/miniconda3/miniconda.sh 설치 후, 새로 설치한 미니콘다를 초기화합니다. 다음 명령은 bash 및 zsh 셸을 초기화 ~/miniconda3/bin/conda init bash ~/miniconda3/bin/conda init zsh django on GCE GCE Shell에서 django를 설치한다.

Spark Code 실행 예제

강의소개 인프런에서 Streamlit 관련 강의를 진행하고 있습니다. 인프런 : https://inf.run/YPniH 개요 현재 러닝 스파크 교재를 배우고 있다. 해당 교재는 주로 00.py에서 실행하는 방법으로 안내하고 있지만, Google Colab에서 어떻게 변환하는지 확인해보고자 한다. Spark 설정 Spark 설치 버전은 아래 링크에서 확인한다. 주소 : https://spark.apache.org/downloads.html Download 버튼을 클릭하면 아래와 같은 화면이 나온다. 주소를 복사한다. https://dlcdn.apache.org/spark/spark-3.5.1/spark-3.5.1-bin-hadoop3.tgz Java 설치 아래 코드를 실행한다. !apt-get install openjdk-8-jdk-headless Spark 설치 아래 코드를 실행한다. !wget -q https://dlcdn.apache.org/spark/spark-3.5.1/spark-3.5.1-bin-hadoop3.tgz !tar -zxf spark-3.

Pandas DataFrame to MySQL Database using iris Data

개요 이전 강의에 이어서 진행한다. (MySQL Select Clause via Python) 임의의 Pandas 데이터 프레임에서 MySQL DB로 추가하는 코드를 작성한다. 주요 라이브러리 설치 아래와 같이 주요 라이브러리를 설치한다. MySQL과 관련된 주요 Python 라이브러리를 설치한다. pip install mysql-connector mysql-connector-python pymysql SQLAlchemy seaborn pandas 코드 작성(mysql-connector) 아래와 같이 코드를 작성한다. # 파일명 : db.py import mysql.connector import pandas as pd import seaborn as sns mydb = mysql.connector.connect( host = "localhost", user = "root", passwd = "evan", database = "muldb" ) print(mydb) iris_df = sns.

MySQL Select Clause via Python

개요 이전 강의에서 출발한다. MySQL Table Creation and Insert Data via Python 데이터 조회 다음 코드를 작성한다. import mysql.connector mydb = mysql.connector.connect( host = "localhost", user = "root", passwd = "evan", database = "mulcampdb" ) print(mydb) my_cursor = mydb.cursor() query = """ SELECT * FROM users; """ my_cursor.execute(query) result = my_cursor.fetchall() for row in result: print(row) print("완료") 파일을 실행한다. $ python database.py <mysql.connector.connection_cext.CMySQLConnection object at 0x000001FE5A985F10> ('Evan', 'Evan@gmail.com', 30, 1) ('Evan', 'Evan@gmail.

MySQL Table Creation and Insert Data via Python

개요 이전 강의에서 출발한다. Connect To Database in Python 테이블 생성 아래 코드를 작성하면 테이블이 생성된다. import mysql.connector mydb = mysql.connector.connect( host = "localhost", user = "root", passwd = "evan", database = "mulcampdb" ) print(mydb) my_cursor = mydb.cursor() query = """ CREATE TABLE users ( name VARCHAR(255) , email VARCHAR(255) , age INTEGER(10) , user_id INTEGER AUTO_INCREMENT PRIMARY KEY ); """ my_cursor.execute(query) my_cursor.execute("SHOW TABLES;") for table in my_cursor: print(table[0]) 파일을 실행한다.

Connect To Database in Python

개요 Python과 MySQL을 연동하도록 한다. 프로젝트 폴더에 가상환경이 설치가 되어 있는 것으로 가정한다. MySQL은 기 설치가 되어 있는 것으로 가정한다. 라이브러리 설치 Python과 MySQL을 연동해주는 라이브러리 종류는 다양하게 있다. $ pip install mysql-connector mysql-connector-python 파일 작성 간단하게 파일을 작성한다. import mysql.connector mydb = mysql.connector.connect( host = "localhost", user = "root", passwd = "evan" ) print(mydb) 파일을 실행한다. $ python database.py <mysql.connector.connection_cext.CMySQLConnection object at 0x000002BF4E606090> (venv) Python 코드 활용하여 DB 생성 이번에는 코드를 활용하여 Schema를 생성한다.

Github Actions Hello World From Python Script

개요 Python Script를 활용하여 Hell World를 출력한다. 강의소개 인프런에서 Streamlit 관련 강의를 진행하고 있습니다. 인프런 : https://inf.run/YPniH 이전 게시글 링크 확인 : Github Actions Hello World main.py 작성 간단하게 아래 코드를 작성한다. 코드 작성은 Github에서도 가능하다. import sys print(sys.version) print("Hello, World") Add file > Create new file 버튼을 클릭한다. Python-hello.yml 파일 변경 기존 코드에서 다음 코드를 추가한다. # This is a basic workflow to help you get started with Actions name: Python-CI .

Github Actions Hello World

개요 Github Actions 에서 Hello World를 출력하도록 한다. 강의소개 인프런에서 Streamlit 관련 강의를 진행하고 있습니다. 인프런 : https://inf.run/YPniH 사전준비 Github에 적당한 Repo를 준비한다. 메뉴선택 아래 그림에서 Actions 메뉴를 선택한다. 아래 그림에서 set up a workflow yourself 선택 YAML 파일 수정 .github/workflows/main.yaml 파일 선택 후 수정 소스코드는 다음과 같이 지정한다. # This is a basic workflow to help you get started with Actions name: CI # Controls when the workflow will run on: # Triggers the workflow on push or pull request events but only for the "main" branch push: branches: [ "main" ] pull_request: branches: [ "main" ] # Allows you to run this workflow manually from the Actions tab workflow_dispatch: # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: # This workflow contains a single job called "build" build: # The type of runner that the job will run on runs-on: ubuntu-latest # Steps represent a sequence of tasks that will be executed as part of the job steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v3 # Runs a single command using the runners shell - name: Run a one-line script run: echo Hello, world!

Streamlit ML Multiclass Classification Model Prediction Sample (feat. Pipeline)

개요 Kaggle 데이터셋을 활용하여 Streamlit ML Multiclass Classification Model을 배포한다. 각 코드에 대한 자세한 설명은 여기에서는 생략한다. 데이터 수집 이번에 활용하는 캐글 데이터 수집은 아래 대회에서 train 데이터만 가져왔다. Multi-Class Prediction of Obesity Risk : https://www.kaggle.com/competitions/playground-series-s4e2 Dataset Description은 아래에서 확인하도록 한다. 링크 : https://www.kaggle.com/competitions/playground-series-s4e2/data train.csv 파일만 다운로드 받았다. 모델 개발 다음 코드는 모델을 개발하는 코드이다. 주어진 데이터셋에서 종속변수 NObeyesdad을 예측하는 모델을 구성했다. 파일명 : model.py import pandas as pd from sklearn.

Streamlit ML Model Prediction Sample (feat. Pipeline)

강의소개 인프런에서 Streamlit 관련 강의를 진행하고 있습니다. 인프런 : https://inf.run/YPniH 개요 tips 데이터셋을 활용하여 Streamlit ML Model을 배포한다. 각 코드에 대한 자세한 설명은 여기에서는 생략한다. 모델 개발 다음 코드는 모델을 개발하는 코드이다. 주어진 데이터셋에서 tip을 예측하는 모델을 구성했다. 파일명 : model.py import streamlit as st import pandas as pd import seaborn as sns from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.compose import ColumnTransformer from sklearn.pipeline import Pipeline from sklearn.