쿠버네티스,쿠버플로우

던파 데이터 Django 부분

세용용용용 2023. 9. 21. 13:52

0. manage.py

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

 

1. mysite

asgi.py

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

application = get_asgi_application()

 

settings.py

"""
Django settings for config project.

Generated by 'django-admin startproject' using Django 4.0.3.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-lmjyy31)m$p*pz2-wh!6ystktl^06g(go&pr@_$*34df^1y20e'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pybo',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME' : 'donpa_item',
        'USER' : 'root',
        'PASSWORD' : '1234', #MariaDB 로그인 시 비밀번호
        'HOST' : '10.233.18.183', #디폴트는 로컬호스트
        'PORT' : '3306', #기본은 3306인데
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'ko-kr'

TIME_ZONE = 'Asia/Seoul'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

 

urls.py

"""config URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    # 기존의 pybo 앱 URL 매핑
    path('pybo/', include('pybo.urls')),
    # 추가적으로 루트 URL('/')로 접속시 pybo 앱의 URL 매핑을 처리하도록 설정
    path('', include('pybo.urls')),
    path('admin/', admin.site.urls),
]

 

wsgi.py

"""
WSGI config for config project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
"""


import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')

application = get_wsgi_application()

 

 

2. pybo

apps.py

from django.apps import AppConfig


class PyboConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'pybo'

 

models.py

from django.db import models


class DonpaItem1(models.Model):
    item_img = models.TextField(blank=True, null=True)
    item_name = models.TextField(blank=True, null=False, primary_key=True)  # null=False로 수정
    price = models.FloatField(blank=True, null=True)
    before_now = models.FloatField(blank=True, null=True)
    before_one = models.FloatField(blank=True, null=True)
    before_two = models.FloatField(blank=True, null=True)
    before_three = models.FloatField(blank=True, null=True)
    before_four = models.FloatField(blank=True, null=True)
    before_five = models.FloatField(blank=True, null=True)
    before_six = models.FloatField(blank=True, null=True)
    before_seven = models.FloatField(blank=True, null=True)
    before_eight = models.FloatField(blank=True, null=True)
    before_nine = models.FloatField(blank=True, null=True)
    before_ten = models.FloatField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'donpa_item1'


class DonpaNews(models.Model):
    photo = models.TextField(blank=True, null=True)
    title = models.TextField(blank=True, null=True)
    link = models.TextField(blank=True, null=False, primary_key=True)

    class Meta:
        managed = False
        db_table = 'donpa_news'


class DonpaEvent(models.Model):
    img = models.TextField(blank=True, null=True)
    text = models.TextField(blank=True, null=True)
    date = models.TextField(blank=True, null=True)
    herf = models.TextField(blank=True, null=False, primary_key=True)

    class Meta:
        managed = False
        db_table = 'donpa_event'


class InputList(models.Model):
    title = models.TextField(blank=True, null=False, primary_key=True)
    jobname = models.TextField(blank=True, null=True)
    emblem = models.TextField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'input_list'


class InputList1(models.Model):
    title = models.TextField(blank=True, null=False, primary_key=True)
    jobname = models.TextField(blank=True, null=True)
    emblem = models.TextField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'input_list1'


class Goldprice(models.Model):
    date = models.TextField(blank=True, null=False, primary_key=True)
    sell = models.FloatField(blank=True, null=True)
    buy = models.FloatField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'goldprice'

python manage.py makemigrations

python manage.py migrate

 

 

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.item_list),
    path('aabata/', views.aabata_view, name='aabata'),
    path('process_data/', views.process_data, name='process_data'),  # "/process_input/" URL 패턴 추가
    path('process_data1/', views.process_data1, name='process_data1'),  # "/process_input/" URL 패턴 추가
    path('event/', views.events_view, name='event'),
    path('news/', views.news_view, name='news'),  # /news/ 경로에 news_view를 연결
    
    
    # 다른 URL 패턴들...
]

 

views.py

from django.shortcuts import render
from .models import DonpaItem1
from .models import DonpaNews
from .models import DonpaEvent
from .models import InputList
from .models import InputList1
from .models import Goldprice


def item_list(request):
    items = DonpaItem1.objects.all()
    return render(request, 'index.html', {'items': items})

def aabata_view(request):
    # aabata.html 템플릿 렌더링
    items = InputList.objects.all()
    items1 = InputList1.objects.all()
    latest_goldprice = Goldprice.objects.latest('date')

    # 숫자 데이터를 텍스트로 변환
    
    sell_text = str(latest_goldprice.sell)
    buy_text = str(latest_goldprice.buy)

    return render(request, 'aabata.html', {'items': items, 
                                           'items1': items1, 
                                           'sell_text': sell_text,
                                           'buy_text': buy_text})

def events_view(request):
    # events.html 템플릿 렌더링
    items = DonpaEvent.objects.all()
    return render(request, 'event.html', {'items':items})    

def news_view(request):
    # news.html 템플릿 렌더링
    items = DonpaNews.objects.all()
    return render(request, 'news.html', {'items':items})


import pandas as pd
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
import json

@csrf_exempt
def process_data(request):
    if request.method == 'POST':
        try:
            # POST 요청에서 데이터 추출
            data = json.loads(request.body.decode('utf-8'))
            selectedTitle = data['selectedTitle']
            selectedJobname = data['selectedJobname']
            selectedEmblem = data['selectedEmblem']
            sell = data['sell']
            buy = data['buy']
            year = data['year']
            month = data['month']
            day = data['day']
            day_name = data['day_name']

            # 데이터 프레임 생성
            df = pd.DataFrame({
                'title': [selectedTitle],
                'jobname': [selectedJobname],
                'emblem': [selectedEmblem],
                'sell': [sell],
                'buy': [buy],
                'year': [year],
                'month': [month],
                'day': [day],
                'day_name': [day_name]
            })

            # 여기에서 데이터프레임을 원하는 방식으로 처리
            df['sell'] = df['sell'].astype(float)
            df['buy'] = df['buy'].astype(float)
            df['year'] = df['year'].astype(int)
            df['month'] = df['month'].astype(int)
            df['day'] = df['day'].astype(int)

            # 데이터프레임 스케일링
            obj_col = df.select_dtypes(include='object').columns
            from sklearn.preprocessing import StandardScaler

            # 스케일러 불러오기
            import joblib
            sds = joblib.load('/home/jovyan/scaler/scaler.pkl')


            df_sc = sds.transform(df.drop(columns = obj_col))
            df_sc = pd.DataFrame(df_sc, columns = df.drop(columns = obj_col).columns)

            # object 타입 컬럼 붙여주기
            for i in obj_col:
                df_sc[i] = df[i]

            # 원핫 인코딩 돌려주자
            from sklearn.preprocessing import OneHotEncoder
            encoder = joblib.load('/home/jovyan/encoder/encoder.pkl')

            
            # 범주형 열만 선택
            obj_df = df_sc.select_dtypes(include='object')
            # 숫자형 열만 선택
            no_obj_df = df_sc.select_dtypes(exclude='object')
            # 범주형 열을 원핫 인코딩
            encoded_features = encoder.transform(obj_df)

            # 인코딩된 결과를 데이터프레임으로 변환
            encoded_df = pd.DataFrame(encoded_features.toarray(), columns=encoder.get_feature_names(obj_df.columns))
            # 인코딩된 범주형 열과 숫자형 열을 합침
            df_sc_encoding = pd.concat([no_obj_df[:len(df_sc)] , encoded_df[:len(df_sc)]], axis = 1)
            

            # 컬럼 특수문자 제거
            import re
            # 데이터프레임의 컬럼 이름에서 특수 문자를 제거하고 변경할 새로운 컬럼 이름 리스트 생성
            new_columns = []
            for old_column in df_sc_encoding.columns:
                new_column = re.sub(r'[^\w\s]', '', old_column)  # 특수 문자 제거
                new_columns.append(new_column)

            # 컬럼 이름을 새로운 이름으로 설정
            df_sc_encoding.columns = new_columns
            

            # 모델을 가져와 predict하자
            import xgboost as xgb
            xgb = joblib.load('/home/jovyan/model/model.pkl')
            pred_result = xgb.predict(df_sc_encoding)[0]

            pred_result = str(pred_result)
            
            # 결과를 JSON 형식으로 반환
            result = {'message': pred_result}
            return JsonResponse(result)
        except Exception as e:
            return JsonResponse({'message': 'Error: {}'.format(str(e))})
    else:
        return JsonResponse({'message': 'Invalid request method'})









import pandas as pd
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
import json

@csrf_exempt
def process_data1(request):
    if request.method == 'POST':
        try:
            # POST 요청에서 데이터 추출
            data = json.loads(request.body.decode('utf-8'))
            selectedTitle = data['selectedTitle']
            selectedJobname = data['selectedJobname']
            selectedEmblem = data['selectedEmblem']
            sell = data['sell']
            buy = data['buy']
            year = data['year']
            month = data['month']
            day = data['day']
            day_name = data['day_name']

            # 데이터 프레임 생성
            df = pd.DataFrame({
                'title': [selectedTitle],
                'jobname': [selectedJobname],
                'emblem': [selectedEmblem],
                'sell': [sell],
                'buy': [buy],
                'year': [year],
                'month': [month],
                'day': [day],
                'day_name': [day_name]
            })

            # 여기에서 데이터프레임을 원하는 방식으로 처리
            df['sell'] = df['sell'].astype(float)
            df['buy'] = df['buy'].astype(float)
            df['year'] = df['year'].astype(int)
            df['month'] = df['month'].astype(int)
            df['day'] = df['day'].astype(int)

            # 데이터프레임 스케일링
            obj_col = df.select_dtypes(include='object').columns
            from sklearn.preprocessing import StandardScaler

            # 스케일러 불러오기
            import joblib
            sds = joblib.load('/home/jovyan/scaler/scaler.pkl')

            df_sc = sds.transform(df.drop(columns = obj_col))
            df_sc = pd.DataFrame(df_sc, columns = df.drop(columns = obj_col).columns)

            # object 타입 컬럼 붙여주기
            for i in obj_col:
                df_sc[i] = df[i]

            # 원핫 인코딩 돌려주자
            from sklearn.preprocessing import OneHotEncoder
            encoder = joblib.load('/home/jovyan/encoder/encoder.pkl')
     
            # 범주형 열만 선택
            obj_df = df_sc.select_dtypes(include='object')
            # 숫자형 열만 선택
            no_obj_df = df_sc.select_dtypes(exclude='object')
            # 범주형 열을 원핫 인코딩
            encoded_features = encoder.transform(obj_df)

            # 인코딩된 결과를 데이터프레임으로 변환
            encoded_df = pd.DataFrame(encoded_features.toarray(), columns=encoder.get_feature_names(obj_df.columns))
            # 인코딩된 범주형 열과 숫자형 열을 합침
            df_sc_encoding = pd.concat([no_obj_df[:len(df_sc)] , encoded_df[:len(df_sc)]], axis = 1)
            

            # 컬럼 특수문자 제거
            import re
            # 데이터프레임의 컬럼 이름에서 특수 문자를 제거하고 변경할 새로운 컬럼 이름 리스트 생성
            new_columns = []
            for old_column in df_sc_encoding.columns:
                new_column = re.sub(r'[^\w\s]', '', old_column)  # 특수 문자 제거
                new_columns.append(new_column)

            # 컬럼 이름을 새로운 이름으로 설정
            df_sc_encoding.columns = new_columns
            

            # 모델을 가져와 predict하자
            import xgboost as xgb
            xgb = joblib.load('/home/jovyan/model/model.pkl')
            pred_result = xgb.predict(df_sc_encoding)[0]

            pred_result = str(pred_result)
            
            # 결과를 JSON 형식으로 반환
            result = {'message': pred_result}
            return JsonResponse(result)
        except Exception as e:
            return JsonResponse({'message': 'Error: {}'.format(str(e))})
    else:
        return JsonResponse({'message': 'Invalid request method'})

 

 

이미지로 만들기

dockerfile

# base image
FROM python:3.9

# 설정할 작업 디렉토리
WORKDIR /app

# 호스트의 현재 디렉토리의 모든 파일을 작업 디렉토리로 복사
COPY donpa/ /app
COPY requirements.txt /app

# 필요한 종속성 설치
RUN pip install --no-cache-dir -r requirements.txt

RUN mkdir scaler
RUN mkdir encoder
RUN mkdir model

# 컨테이너 실행 시 실행할 명령어
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
~

 

requirements.txt

asgiref==3.7.2
pytz
Django==4.0.3
joblib==1.1.1
mysqlclient==2.2.0
numpy
pandas==1.1.5
PyMySQL==1.1.0
python-dateutil==2.8.2
pytz==2023.3.post1
scikit-learn==0.24.2
scipy==1.5.4
six==1.16.0
sqlparse==0.4.4
typing-extensions==4.7.1
xgboost==1.5.2

 

donpa >>> 장고 디렉토리

 

docker build -t >>> 이미지 만들고

docker push >>> 저장소로 푸쉬 시키기