from pymongo import MongoClient
from faker import Faker
# MongoDB에 연결
client = MongoClient('mongodb://localhost:27017/')
# 데이터베이스 선택
db = client['mydatabase_sales']
# 컬렉션 선택
collection = db['sales']
# Faker 객체 생성
fake = Faker()
# 가상의 매출 데이터 생성 및 입력
for _ in range(20):
sale = {
'product': fake.word(),
'amount': fake.random_int(min=1000, max=10000)
}
collection.insert_one(sale)
print("가상의 매출 데이터가 성공적으로 입력되었습니다.")
import streamlit as st
from pymongo import MongoClient
import pandas as pd
import matplotlib.pyplot as plt
# MongoDB에 연결
client = MongoClient('mongodb://localhost:27017/')
# 데이터베이스 선택
db = client['mydatabase_sales']
# 컬렉션 선택
collection = db['sales']
def get_sales_data():
sales_data = collection.find()
df = pd.DataFrame(sales_data)
return df
# 데이터 조회
st.header('매출 데이터 조회')
df_sales = get_sales_data()
st.dataframe(df_sales)
# 데이터 통계
st.subheader('매출 통계')
st.write('총 매출:', df_sales['amount'].sum())
st.write('평균 매출:', df_sales['amount'].mean())
# 데이터 시각화
st.subheader('매출 시각화')
plt.figure(figsize=(10, 6))
plt.bar(df_sales['product'], df_sales['amount'])
plt.xlabel('product')
plt.ylabel('amount')
plt.xticks(rotation=45)
st.pyplot(plt)