fullcalendar_test.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from nicegui import ui
  2. from datetime import datetime
  3. def format_date(date_str):
  4. # Parse the date string and format it consistently
  5. parsed_date = datetime.fromisoformat(date_str)
  6. return parsed_date.strftime('%Y-%m-%d %H:%M:%S')
  7. def create_fullcalendar():
  8. ui.add_head_html("<script src='https://cdn.jsdelivr.net/npm/fullcalendar@6.1.9/index.global.min.js'></script>")
  9. options = {
  10. "initialView": 'timeGridWeek',
  11. "slotMinTime": "05:00:00",
  12. "slotMaxTime": "22:00:00",
  13. "allDaySlot": False,
  14. "timeZone": 'local',
  15. "height": 'auto',
  16. "events": []
  17. }
  18. return ui.fullcalendar(options, on_click=handle_calendar_click)
  19. def handle_calendar_click(event):
  20. try:
  21. start = format_date(event.args['info']['event']['start'])
  22. end = format_date(event.args['info']['event']['end'])
  23. title = event.args['info']['event']['title']
  24. except Exception as e:
  25. title = None
  26. if title:
  27. show_event_card(title, start, end)
  28. def show_event_card(title, start, end):
  29. card = ui.card().style("background-color: #f0f0f0; position: absolute; z-index: 10000; top: 50%; left: 50%; transform: translate(-50%, -50%);")
  30. with card:
  31. ui.label(title)
  32. ui.button("Click me to remove the event!", on_click=lambda: (fullcal.remove_event(title=title.strip(), start=start, end=end), card.delete()))
  33. ui.button("Close", on_click=lambda e: card.delete())
  34. def add_event():
  35. fullcal.addevent("Math 1b03", format_date("2023-11-18 09:30:00"), format_date("2023-11-18 10:20:00"), color="red")
  36. fullcal = create_fullcalendar()
  37. ui.button("Click me to add event", on_click=add_event)
  38. ui.run()