-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfullcalendar.py
More file actions
71 lines (57 loc) · 2.96 KB
/
fullcalendar.py
File metadata and controls
71 lines (57 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from collections.abc import Callable
from pathlib import Path
from typing import Any
from nicegui import events, ui
class FullCalendar(ui.element, component='fullcalendar.js'):
"""
Provides a NiceGUI element to create a interactive FullCalendar calendar
display. This calendar can be customized with various options and can
handle events such as clicks and view changes. The calendar supports
adding, removing, and clearing events dynamically.
Based on: https://github.com/zauberzeug/nicegui/tree/main/examples/fullcalendar
References:
- FullCalendar library: https://fullcalendar.io/
- FullCalendar documentation: https://fullcalendar.io/docs
- Example of the FullCalendar library with plugins: https://github.com/dorel14/NiceGui-FullCalendar_more_Options
"""
def __init__(self, options: dict[str, Any], custom_css: str, on_click: Callable | None = None, on_change: Callable | None = None) -> None:
"""Creates a FullCalendar component.
:param options: dictionary of FullCalendar properties for customization, such as "initialView", "slotMinTime", "slotMaxTime", "allDaySlot", "timeZone", "height", and "events".
:param custom_css: custom CSS for styling the calendar.
:param on_click: callback that is called when a calendar event is clicked.
:param on_change: callback that is called when the calendar view changes.
"""
super().__init__()
self.add_resource(Path(__file__).parent / 'lib')
self._props['options'] = options
self._props['custom_css'] = custom_css
self._update_method = 'update_calendar'
if on_click:
self.on('click', lambda e: events.handle_event(on_click, e))
if on_change:
self.on('change', lambda e: events.handle_event(on_change, e))
def add_event(self, title: str, start: str, end: str, **kwargs) -> None:
"""Adds an event to the calendar.
:param title: title of the event
:param start: start time of the event
:param end: end time of the event
"""
event_dict = {'title': title, 'start': start, 'end': end, **kwargs}
self._props['options']['events'].append(event_dict)
def remove_event(self, title: str, start: str, end: str) -> None:
"""Removes an event from the calendar.
:param title: title of the event
:param start: start time of the event
:param end: end time of the event
"""
for event in self._props['options']['events']:
if event['title'] == title and event['start'] == start and event['end'] == end:
self._props['options']['events'].remove(event)
break
def clear_events(self) -> None:
"""Removes all events from the calendar."""
self._props['options']['events'] = []
@property
def events(self) -> list[dict]:
"""List of events currently displayed in the calendar."""
return self._props['options']['events']