Create app,py
Browse files
app,py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import pandas as pd
|
4 |
+
from datetime import datetime
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# フォルダーとCSVファイルのパス
|
8 |
+
photos_folder = 'photos'
|
9 |
+
index_csv_path = 'index.csv'
|
10 |
+
|
11 |
+
# フォルダーが存在しない場合は作成
|
12 |
+
if not os.path.exists(photos_folder):
|
13 |
+
os.makedirs(photos_folder)
|
14 |
+
|
15 |
+
# CSVファイルが存在しない場合は初期化
|
16 |
+
if not os.path.exists(index_csv_path):
|
17 |
+
index_df = pd.DataFrame(columns=['FileName', 'TimeStamp'])
|
18 |
+
index_df.to_csv(index_csv_path, index=False)
|
19 |
+
|
20 |
+
# 汎用的な関数: CSVに新しい行を追加
|
21 |
+
def add_to_index(file_name):
|
22 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
23 |
+
new_entry = pd.DataFrame({'FileName': [file_name], 'TimeStamp': [timestamp]})
|
24 |
+
index_df = pd.read_csv(index_csv_path)
|
25 |
+
index_df = pd.concat([index_df, new_entry], ignore_index=True)
|
26 |
+
index_df.to_csv(index_csv_path, index=False)
|
27 |
+
|
28 |
+
# Streamlitアプリケーションの作成
|
29 |
+
def main():
|
30 |
+
st.title('Photo Gallery')
|
31 |
+
|
32 |
+
# 写真のアップロード
|
33 |
+
uploaded_file = st.file_uploader("写真をアップロードしてください", type=["jpg", "jpeg", "png"])
|
34 |
+
|
35 |
+
if uploaded_file is not None:
|
36 |
+
# アップロードされた写真を保存
|
37 |
+
image = Image.open(uploaded_file)
|
38 |
+
file_name = os.path.join(photos_folder, f"{datetime.now().strftime('%Y%m%d%H%M%S')}.png")
|
39 |
+
image.save(file_name)
|
40 |
+
|
41 |
+
# インデックスに新しい行を追加
|
42 |
+
add_to_index(file_name)
|
43 |
+
|
44 |
+
# インデックスの読み込み
|
45 |
+
index_df = pd.read_csv(index_csv_path)
|
46 |
+
|
47 |
+
# ページングと並び替え機能
|
48 |
+
per_page = 20
|
49 |
+
page_number = st.sidebar.number_input('ページ番号', min_value=1, value=1)
|
50 |
+
sort_option = st.sidebar.selectbox('並び替え項目', ['TimeStamp昇順', 'TimeStamp降順', '名前昇順', '名前降順'])
|
51 |
+
|
52 |
+
if sort_option == 'TimeStamp昇順':
|
53 |
+
index_df = index_df.sort_values(by='TimeStamp', ascending=True)
|
54 |
+
elif sort_option == 'TimeStamp降順':
|
55 |
+
index_df = index_df.sort_values(by='TimeStamp', ascending=False)
|
56 |
+
elif sort_option == '名前昇順':
|
57 |
+
index_df = index_df.sort_values(by='FileName', ascending=True)
|
58 |
+
elif sort_option == '名前降順':
|
59 |
+
index_df = index_df.sort_values(by='FileName', ascending=False)
|
60 |
+
|
61 |
+
# ページング
|
62 |
+
start_idx = (page_number - 1) * per_page
|
63 |
+
end_idx = start_idx + per_page
|
64 |
+
display_df = index_df.iloc[start_idx:end_idx]
|
65 |
+
|
66 |
+
# 写真の表示
|
67 |
+
for _, row in display_df.iterrows():
|
68 |
+
st.image(row['FileName'], caption=row['FileName'], use_column_width=True)
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
main()
|