fullcalendar.py 2.1 KB

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