fullcalendar_test.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from nicegui import app, 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. ui.add_head_html("<script src='https://cdn.jsdelivr.net/npm/fullcalendar@6.1.9/index.global.min.js'></script>")
  8. options = {
  9. "initialView": 'timeGridWeek',
  10. "slotMinTime": "05:00:00",
  11. "slotMaxTime": "22:00:00",
  12. "allDaySlot": False,
  13. "timeZone": 'local',
  14. "height": 'auto', "events": []
  15. }
  16. title= None
  17. fullcal = None
  18. def func(e):
  19. global fullcal, title, card
  20. title = None
  21. # print(e)
  22. try:
  23. start = format_date(e.args['info']['event']['start'])
  24. end = format_date(e.args['info']['event']['end'])
  25. title2 = e.args['info']['event']['title']
  26. except:
  27. title2 = None
  28. if title2:
  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(title2)
  32. ui.button("Click me to remove the event!", on_click=lambda : (fullcal.remove_event(title=title2.strip(), start=(start), end=(end)), card.delete()))
  33. ui.button("Close", on_click=lambda e: card.delete())
  34. fullcal = ui.fullcalendar(options, on_click=lambda e: func(e))
  35. def add_event():
  36. global fullcal
  37. fullcal.addevent("Math 1b03", format_date("2023-11-18 09:30:00"), format_date("2023-11-18 10:20:00"), color="red")
  38. def remove_event():
  39. global fullcal
  40. fullcal.remove_event("Math 1b03", format_date("2023-11-18 09:30:00"), format_date("2023-11-18 10:20:00"))
  41. ui.button("click me to add event", on_click=add_event)
  42. ui.run()