fullcalendar.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from typing import Any, Callable, Dict, List, Optional
  2. from ..element import Element
  3. from ..events import handle_event
  4. class FullCalendar(Element, component='fullcalendar.js', libraries=['lib/fullcalendar/index.global.js']):
  5. def __init__(self, options: Dict[str, Any], on_click: Optional[Callable] = None) -> None:
  6. """FullCalendar
  7. An element that integrates the FullCalendar library (https://fullcalendar.io/) to create an interactive calendar display.
  8. :param options: dictionary of FullCalendar properties for customization, such as "initialView", "slotMinTime", "slotMaxTime", "allDaySlot", "timeZone", "height", and "events".
  9. :param on_click: callback function that is called when a calendar event is clicked.
  10. """
  11. super().__init__()
  12. self._props['options'] = options
  13. if on_click:
  14. self.on('click', lambda e: handle_event(on_click, e))
  15. def add_event(self, title: str, start: str, end: str, **kwargs) -> None:
  16. event_dict = {'title': title, 'start': start, 'end': end, **kwargs}
  17. self._props['options']['events'].append(event_dict)
  18. self.update()
  19. self.run_method('update_calendar')
  20. def remove_event(self, title: str, start: str, end: str) -> None:
  21. for event in self._props['options']['events']:
  22. if event['title'] == title and event['start'] == start and event['end'] == end:
  23. self._props['options']['events'].remove(event)
  24. break
  25. self.update()
  26. self.run_method('update_calendar')
  27. @property
  28. def events(self) -> List[Dict]:
  29. return self._props['options']['events']