/streamlit-tutorial

๐Ÿซ Streamlit Tutorial

Primary LanguagePython

Streamlit Tutorial

  • Streamlit Tutorial์€ 2024๋…„ 1ํ•™๊ธฐ ๋ฐ์ดํ„ฐ ์‚ฌ์ด์–ธ์Šค ๊ฐœ๋ก  ์ˆ˜๊ฐ•์ƒ์„ ๋Œ€์ƒ์œผ๋กœ ์ง„ํ–‰๋œ ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค.
  • ์ž‘์„ฑ์ผ์ž: 2024๋…„ 4์›” 26์ผ

ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ ์„ธํŒ…

Python๊ณผ VSC ์„ค์น˜

  • Python์ด ์ปดํ“จํ„ฐ์— ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์˜ ๋ฒ„์ „์€ 3.8 ์ด์ƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.
  • ์ฝ”๋“œ ์—๋””ํ„ฐ์ธ Visual Studio Code๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ด ๋งํฌ๋ฅผ ํด๋ฆญํ•˜๋ฉด, VSC๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ”„๋กœ์ ํŠธ ์†Œ์Šค์ฝ”๋“œ ๋‹ค์šด๋กœ๋“œ

๊นƒํ—™์—์„œ ํ•ด๋‹น ๋ ˆํฌ์ง€ํ† ๋ฆฌ์˜ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ƒ๋‹จ์˜ ์ดˆ๋ก์ƒ‰ Code ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ ํ›„, 'Download ZIP'์„ ํด๋ฆญํ•˜๋ฉด ์†Œ์Šค์ฝ”๋“œ zip ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

VSC๋กœ ํ”„๋กœ์ ํŠธ ํด๋” ์—ด๊ธฐ

์ด ๋ ˆํฌ์ง€ํ† ๋ฆฌ์˜ ๋””๋ ‰ํ† ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

streamlit-tutorial
โ”œโ”€ .gitignore
โ”œโ”€ README.md
โ”œโ”€ data
โ”‚  โ””โ”€ แ„Œแ…ฅแ†ซแ„€แ…ฎแ†จแ„ƒแ…ฉแ„‰แ…ฅแ„€แ…ชแ†ซแ„‘แ…ญแ„Œแ…ฎแ†ซแ„ƒแ…ฆแ„‹แ…ตแ„แ…ฅ.csv
โ”œโ”€ main.py
โ””โ”€ requirements.txt
  • main.py: streamlit์ด ์‹คํ–‰๋  ํŒŒ์ด์ฌ ํŒŒ์ผ๋กœ, ํŠœํ† ๋ฆฌ์–ผ์—์„œ ์ง„ํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • README.md: ํ”„๋กœ์ ํŠธ์˜ ์†Œ๊ฐœ์™€ ์‚ฌ์šฉ๋ฒ•, ์„ค์น˜ ๋ฐฉ๋ฒ• ๋“ฑ์— ๋Œ€ํ•œ ์ „๋ฐ˜์ ์ธ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค.
  • requirements.txt: ์ด ํŒŒ์ผ์€ ํ”„๋กœ์ ํŠธ๊ฐ€ ์˜์กดํ•˜๋Š” ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‚˜ ํŒจํ‚ค์ง€์˜ ๋ชฉ๋ก์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.
  • .gitignore: ์ด ํŒŒ์ผ์€ git ๋ ˆํฌ์ง€ํ† ๋ฆฌ์— ํฌํ•จ๋˜์ง€ ์•Š์•„์•ผ ํ•  ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • data ํด๋”: ํŠœํ† ๋ฆฌ์–ผ์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋งˆํฌ๋‹ค์šด(markdown) ์‚ฌ์šฉํ•˜๊ธฐ

๋งˆํฌ๋‹ค์šด์€ ์ผ๋ฐ˜์ ์ธ ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜์˜ ๊ฒฝ๋Ÿ‰ ๋งˆํฌ์—… ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ๋งˆํฌ๋‹ค์šด์€ ๊ฐ„๋‹จํ•œ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•ด ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งˆํฌ๋‹ค์šด ์‚ฌ์šฉ๋ฒ•

ํ—ค๋”(headers)

ํ—ค๋”๋Š” 1~6๊นŒ์ง€ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

# This is a H1
## This is a H2
### This is a H3
#### This is a H4
##### This is a H5
###### This is a H6
ํ…์ŠคํŠธ ๊ฐ•์กฐ

๋งˆํฌ๋‹ค์šด์€ ๋ณผ๋“œ์ฒด, ์ดํƒค๋ฆญ์ฒด, ์ทจ์†Œ์„  ๋“ฑ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

*italic*
**bold**
~~stroke~~
์ด๋ฏธ์ง€ ์‚ฝ์ž…

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ๋ฅผ ๋„ฃ์–ด ์ด๋ฏธ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

![image title](/image/steve-busch-3aWG50MWn6U-unsplash.jpg)

image title

๋งํฌ ์‚ฝ์ž…

๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ…์ŠคํŠธ์— ์™ธ๋ถ€ ๋งํฌ๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋งํฌ๋ฅผ ํด๋ฆญํ•˜๋ฉด ํŠœํ† ๋ฆฌ์–ผ์˜ ๊นƒํ—™ ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋กœ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค.

[link title](https://github.com/givemetarte/streamlit-tutorial)
์ฝ”๋“œ ๋ธ”๋Ÿญ ์ƒ์„ฑ

๋ฐฑํ‹ฑ(`)์„ ์ด์šฉํ•ด ์ฝ”๋“œ ๋ธ”๋Ÿญ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ ๋ธ”๋Ÿญ ์˜†์— ์–ธ์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, ํ•ด๋‹น ์–ธ์–ด์— ๋งž๋Š” syntax highlighting์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

import pandas as pd

df = pd.read_csv('/data/๊ณต๊ณต๋„์„œ๊ด€ํ‘œ์ค€๋ฐ์ดํ„ฐ.csv', encoding='utf-8')

๋งˆํฌ๋‹ค์šด Preview

VSC์—์„œ ์ž‘์„ฑํ•œ ๋งˆํฌ๋‹ค์šด ํŒŒ์ผ์˜ preview๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์—ด๊ณ ์‹ถ์€ ๋งˆํฌ๋‹ค์šด ํŒŒ์ผ์— ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ ํ›„ open preview๋ฅผ ํด๋ฆญํ•˜๋ฉด, ์ž‘์„ฑํ•œ ๋งˆํฌ๋‹ค์šด ํŒŒ์ผ์ด ์–ด๋–ป๊ฒŒ ๋‚˜ํƒ€๋‚˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑํ•˜๊ธฐ

VSC ์ƒ๋‹จ์˜ 'Terminal'์„ ํด๋ฆญํ•˜๊ณ , 'New Terminal'์„ ๋ˆ„๋ฅด๋ฉด ํ„ฐ๋ฏธ๋„์„ ์—ด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ํ„ฐ๋ฏธ๋„์˜ ์ž…๋ ฅ์ฐฝ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ streamlit์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. venv๋Š” ํŒŒ์ด์ฌ์˜ ๋Œ€ํ‘œ์ ์ธ ๊ฐ€์ƒํ™˜๊ฒฝ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, ํ”„๋กœ์ ํŠธ๋งˆ๋‹ค ๋‹ค๋ฅธ ๋ฒ„์ „์˜ ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ๊ฐœ๋ณ„ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฉ๋ฆฌ๋œ ํ™˜๊ฒฝ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

# 1. generate python bubble
python -m venv env

# 2. activate
source env/bin/activate  # mac
env\Scripts\activate.bat  # window

# 3. install dependencies
pip install -r requirements.txt

# 4. deactivate bubble
deactivate

Streamlit ์‹คํ–‰ํ•˜๊ธฐ

์œ„์˜ ๊ฐ€์ƒํ™˜๊ฒฝ์ด ์‹คํ–‰๋œ ์ƒํƒœ์—์„œ ๋‹ค์Œ์˜ ์ฝ”๋“œ๋ฅผ ํ„ฐ๋ฏธ๋„์— ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ •์ƒ์ ์œผ๋กœ streamlit์ด ์‹คํ–‰๋œ๋‹ค๋ฉด localhost:8501์—์„œ ํŠœํ† ๋ฆฌ์–ผ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

streamlit run main.py