0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

streamlitを用いて,月間カレンダーをWebブラウザに表示する

Posted at

西暦と月を選択すると,Webブラウザ上に月間カレンダーを表示するPythonスクリプト

ポイント

1.西暦と月はプルダウンメニューで選択
2.サイズ可変(幅と高さをピクセル指定)
3.月曜日始まり
4.土曜日は青色,日曜日は赤色で表示
スクリーンショット 2025-05-07 8.03.54.jpg

<実行方法>
コマンドプロンプトで,以下をタイプして実行
streamlit run disp_month.py

disp_month.py
# -*- coding: utf_8 -*-
import streamlit as st
import calendar
from datetime import date

def create_monthly_calendar(year, month, day_width, day_height, font_size):
    """月間カレンダーを作成する関数"""
    cal = calendar.Calendar(firstweekday=0)  # 月曜日を週の始まりとする
    month_days = cal.monthdayscalendar(year, month)
    month_name = calendar.month_name[month]
    day_names = ["", "", "", "", "", "", ""]

    st.subheader(f"{year}{month}")

    # カレンダーのヘッダー(曜日)
    cols = st.columns(7)
    for i, day_name in enumerate(day_names):
        cols[i].markdown(f"<div style='text-align: center;'>{day_name}</div>", unsafe_allow_html=True)

    for week in month_days:
        cols = st.columns(7)
        for i, day in enumerate(week):
            if day == 0:
                cols[i].markdown(f"<div style='width: {day_width}px; height: {day_height}px;'></div>", unsafe_allow_html=True)
            else:
                day_style = f"width: {day_width}px; height: {day_height}px; border: 1px solid #ccc; text-align: left; vertical-align: top; font-size: {font_size}px; padding-left: 2px;"
                if i == 5:  # 土曜日
                    day_style += " background-color: #e0f2f7; color: blue;"
                elif i == 6:  # 日曜日
                    day_style += " background-color: #ffe0b2; color: red;"

                cols[i].markdown(f"<div style='{day_style}'>{day}</div>", unsafe_allow_html=True)

st.title("月間カレンダー表示")

current_year = date.today().year
years = list(range(current_year - 10, current_year + 11))
months = list(range(1, 13))

selected_year = st.selectbox("西暦を選択してください", years, index=years.index(current_year))
selected_month = st.selectbox("月を選択してください", months, index=date.today().month - 1)

day_width = st.number_input("各日の幅(ピクセル)", min_value=20, value=50)
day_height = st.number_input("各日の高さ(ピクセル)", min_value=20, value=50)
font_size = st.number_input("フォントサイズ(ピクセル)", min_value=8, value=12)

if st.button("カレンダーを表示"):
    create_monthly_calendar(selected_year, selected_month, day_width, day_height, font_size)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?