콘텐츠로 이동

탭 메뉴로 DB 조회하기

필요한 모듈 설치하기

!pip install openai
!pip install gradio

탭 메뉴 템플릿

import numpy as np
import gradio as gr

with gr.Blocks() as demo:
    gr.Markdown("# 고객 요청 리스트")

    with gr.Tab("탭1"):
        with gr.Row():
            gr.Label("첫번째 탭입니다.", label="설명")

    with gr.Tab("탭2"):
        with gr.Row():
            gr.Label("두번째 탭입니다.", label="설명")

demo.launch()

고객 요청 리스트 탭 준비하기

import numpy as np
import gradio as gr

with gr.Blocks() as demo:
    gr.Markdown("# 고객 요청 리스트")
    gr.Markdown("고객이 요청한 데이터를 표시합니다.")

    with gr.Tab("버튼으로 조회"):
        with gr.Row():
            gr.Label("첫번째 탭입니다.", label="설명")

    with gr.Tab("필터로 조회"):
        with gr.Row():
            gr.Label("두번째 탭입니다.", label="설명")

    with gr.Tab("통계 그래프"):
        with gr.Row():
            gr.Label("세번째 탭입니다.", label="설명")

    with gr.Tab("CSV 저장"):
        with gr.Row():
            gr.Label("네번째 탭입니다.", label="설명")

demo.launch()

버튼 조회 탭 메뉴 구현하기

import gradio as gr
import sqlite3
import pandas as pd

# 버튼 클릭 시 실행될 함수 정의
def button1_action():
    return select_db('기술지원')

def button2_action():
    return select_db('영업')

def button3_action():
    return select_db('생산')

# SQLite 데이터베이스에서 분야별로 데이터 가져오는 함수
def select_db(subject):

    # SQLite 데이터베이스에 연결 (파일이 없다면 자동으로 생성됨)
    conn = sqlite3.connect('customer.db')  # 'customer.db'는 데이터베이스 파일 이름

    # 데이터 조회
    query = 'SELECT * FROM as_list where subject = "' + subject + '"'
    df = pd.read_sql_query(query, conn)

    df = df.rename(columns={'title': '제목', 'subject': '분류', 'content': '내용'})
    return df    


with gr.Blocks() as demo:
    gr.Markdown("# 고객 요청 리스트")
    gr.Markdown("고객이 요청한 데이터를 표시합니다.")

    with gr.Tab("버튼으로 조회"):
        with gr.Row():
            button1 = gr.Button("기술지원")
            button2 = gr.Button("영업")
            button3 = gr.Button("생산")

        # 초기 실행
        df = select_db('기술지원')

        # 버튼 클릭 시 실행될 출력 필드
        output = gr.DataFrame(df, label="결과")            

        # 각 버튼에 클릭 이벤트와 출력 연결
        button1.click(fn=button1_action, outputs=output)
        button2.click(fn=button2_action, outputs=output)
        button3.click(fn=button3_action, outputs=output)

    with gr.Tab("필터로 조회"):
        with gr.Row():
            gr.Label("두번째 탭입니다.", label="설명")

    with gr.Tab("통계 그래프"):
        with gr.Row():
            gr.Label("세번째 탭입니다.", label="설명")

    with gr.Tab("CSV 저장"):
        with gr.Row():
            gr.Label("네번째 탭입니다.", label="설명")

demo.launch()

필터 조회 탭메뉴 완성하기

import gradio as gr
import sqlite3
import pandas as pd

# 버튼 클릭 시 실행될 함수 정의
def button1_action():
    return select_db('기술지원')

def button2_action():
    return select_db('영업')

def button3_action():
    return select_db('생산')

# SQLite 데이터베이스에서 분야별로 데이터 가져오는 함수
def select_db(subject):

    # SQLite 데이터베이스에 연결 (파일이 없다면 자동으로 생성됨)
    conn = sqlite3.connect('customer.db')  # 'customer.db'는 데이터베이스 파일 이름

    # 데이터 조회
    query = 'SELECT * FROM as_list where subject = "' + subject + '"'
    df = pd.read_sql_query(query, conn)

    df = df.rename(columns={'title': '제목', 'subject': '분류', 'content': '내용'})
    return df    


with gr.Blocks() as demo:
    gr.Markdown("# 고객 요청 리스트")
    gr.Markdown("고객이 요청한 데이터를 표시합니다.")

    with gr.Tab("버튼으로 조회"):
        with gr.Row():
            button1 = gr.Button("기술지원")
            button2 = gr.Button("영업")
            button3 = gr.Button("생산")

        # 초기 실행
        df = select_db('기술지원')

        # 버튼 클릭 시 실행될 출력 필드
        output = gr.DataFrame(df, label="결과")            

        # 각 버튼에 클릭 이벤트와 출력 연결
        button1.click(fn=button1_action, outputs=output)
        button2.click(fn=button2_action, outputs=output)
        button3.click(fn=button3_action, outputs=output)

    with gr.Tab("필터로 조회"):
        with gr.Row():
            subject = gr.Dropdown(["기술지원", "영업", "생산"], value="기술지원", label="분류")

        # 초기 실행
        df = select_db('기술지원')

        # 버튼 클릭 시 실행될 출력 필드
        output = gr.DataFrame(df, label="결과")

        # 드롭다운 값 변경 시 필터링된 데이터로 DataFrame 업데이트
        subject.change(fn=select_db, inputs=subject, outputs=output)

    with gr.Tab("통계 그래프"):
        with gr.Row():
            gr.Label("세번째 탭입니다.", label="설명")

    with gr.Tab("CSV 저장"):
        with gr.Row():
            gr.Label("네번째 탭입니다.", label="설명")

demo.launch()