Python 가상 환경 구성
- 파이썬 설치와 별도로 의존성을 관리할 수 있다
- 독립적인 환경, 의존성 관리
실습
pyhton -m vvenv <env_name>
위의 명령어로 가상환경을 만든다.

다음은 extensions를 깔아야 한다.
python

위에서 만든 가상환경을 켜보자
venv\Scripts\activate

앞에 (가상환경이름) 이 생겼다면 정상적으로 들어가진 것이다.
가상환경 내에는 streamlit이 없으므로 다시 한 번 깔아준다 (pip install streamlit)
streamlit run week6.py
스트림릿을 구동시켜서 가상환경 내에서도 잘 작동하는지 확인한다
streamlit을 끄고 싶을 때는 ctrl + c 해주면 된다
정상작동을 확인했으면 Azure 계정과 연결해보자.
먼저 다시 Extension을 설치한다.

Azure App Service를 설치하고 나면

아래에 Azure 로고가 생긴 걸 확인할 수 있다.
로고를 누르고 Ctrl shift p > signin을 통해 azure 계정을 로그인해준다
로그인에 성공하면 아래와 같이 내 계정의 리소스들을 확인할 수 있다.

이제 데이터베이스와 연동해 간단한 투두 기능을 만들고 배포해 보자
CREATE TABLE todos(
id INT auto_inncrement primary key,
task VARCHAR(255) NOT NULL,
craeted_At TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
completed BOOLEAN DEFAULT FALSE.
)
위의 쿼리문을 통해 테이블을 만들어 주었다.
#app.py
import streamlit as st
import mysql.connector
from mysql.connector import Error
# MYSQL과 연결을 한다.
def get_db_connection():
return mysql.connector.connect(
host="엔드포인트",
user="계정",
password="비밀번호",
database="데이터베이스 이름",
use_pure=True
)
# Task를 저장하는 함수
def add_task(task):
try:
connection=get_db_connection()
cursor=connection.cursor()
query="INSERT INTO todos(task) VALUES(%s)"
cursor.execute(query, (task,))
connection.commit()
cursor.close()
connection.close()
except Error as e:
st.error(f"Error: {e}")
# Task 목록을 가져오는 함수
def get_task():
try:
connection=get_db_connection()
cursor=connection.cursor()
cursor.execute("SElECT * FROM todos WHERE completed=FALSE ORDER BY created_at DESC")
tasks=cursor.fetchall()
cursor.close()
connection.close()
return tasks
except Error as e:
str.error(f"Error: {e}")
return []
# 작업을 완료하는 부분
def mark_task_completed(task_id):
try:
connection=get_db_connection()
cursor=connection.cursor()
query="UPDATE todos SET completed=TRUE WHERE id=%s"
cursor.execute(query, (task_id,))
connection.commit()
cursor.close()
connection.close()
except Error as e:
st.error(f"Error: {e}")
# User Interface
st.header("Welcome to Super Todo App", divider="rainbow")
# 새로운 todo를 입력하는 부분
new_task=st.text_input("New task")
if st.button("Add Task") and new_task:
add_task(new_task)
st.success(f"Task '{new_task}' added!")
# task를 완료하는 부분
task_id=st.text_input("task id")
if st.button("Complete") and task_id:
mark_task_completed(task_id)
st.success(f"Task '{task_id}' updated!")
tasks=get_task()
st.write(tasks)
database와 연결하고 간단한 todo를 기능을 구현했다.
#streamlit.sh
pip install streamlit
python -m stramlit run app.py --server.port 8000 -—server.address 0.0.0.0
시작 스크립트 파일을 만든다.
#.deployment
[config]
SCM_DO_BUILD_DURING_DEPLOYMENT=false
배포 동안에는 빌드/설치를 하지 말라는 스크립트를 작성했다.

스택 설정에서 시작 명령에 streamlit.sh의 경로를 넣어준다
웹앱>설정>구성>스택설정>시작명령
bash/home/site/wwwroot/streamlit.sh
위의 작업이 모두 끝났으면 배포를 해 주자
vscode에서 Azure 아이콘을 누르고, 내가 만든 웹앱을 찾는다.
우클릭을 누르면 사진과 같은 창이 뜨는데, Deploy to Web App을 눌러 주면 된다

배포는 시간이 꽤 걸린다.
배포 이후에는 도메인으로 잘 접속이 되는지 확인하면 된다.