Calendar #1878
Replies: 5 comments 10 replies
-
#!/usr/bin/env python3
import datetime
from nicegui import ui, app
@ui.page("/")
def main():
## 9.5 == 9:30
# 9:30 - 5:00 = 4.5
# 4.5 / 18 = 0.25
with ui.row().style("width: 100%; height: 100%;"):
with ui.column().style("width:10%; height: 100%; "):
ui.label("Hours").style(f"height: {100/18}%")
for hour in range(5, 23):
ui.label(str(hour)+ ":00").style(f"height: {100 / 18}%; position: absolute; top: {((hour - 5) / 18) * 100}%;")
with ui.column().style("width:10%; height: 100%; display:grid; "):
ui.label("Monday").style(f"height: {100/18}%;")
display_events([
{"name": "Math", "start_time": 9.5, "duration": 1, "color": "red"},
{"name": "Science", "start_time": 11, "duration": 2, "color": "blue"},
{"name": "Stuff", "start_time": 14.5, "duration": 0.8, "color": "blue"}
])
def display_events(events):
start_time = datetime.time(5)
end_time = datetime.time(22)
total_hours = (datetime.datetime.combine(datetime.date.today(), end_time) -
datetime.datetime.combine(datetime.date.today(), start_time)).total_seconds() / 3600
for event in events:
percent_down = (event['start_time'] - 5) / (total_hours+1) * 100
height_of_event = (event['duration'] / (total_hours+1)) * 100
ui.label(event['name']).style(f"position: absolute; top: {percent_down }%; height: {height_of_event}%; width: 10%; background-color: {event['color']};")
ui.run(title="Calendar") This approach appears to be the closest that I can get it, however I'm sure that there's a better way. I don't know web dev as well as I should, but I'm sure that there's a way to overlap a div / textbox / button over a calendar element somehow. This also still needs to get gridlines in there, and i'm near certain that it's not 100% accurate. |
Beta Was this translation helpful? Give feedback.
-
To be honest, it's been a while since I've worked with nicegui, so I'll have to figure out how to run Javascript elements with nicegui again. |
Beta Was this translation helpful? Give feedback.
-
OHHH this is so close to how I want it 😄 from nicegui import ui, client, app
@ui.page("/")
def main():
# Create an HTML container for the FullCalendar
calendar_container = ui.html('<div id="calendar"></div>').style("width: 100%; height: 600px;")
# Make sure the client is connected before loading and initializing FullCalendar
app.on_connect(load_and_initialize_fullcalendar)
def load_and_initialize_fullcalendar():
ui.run_javascript('''
// Load jQuery
let jqueryScript = document.createElement("script");
jqueryScript.src = "https://code.jquery.com/jquery-3.6.0.min.js";
document.body.appendChild(jqueryScript);
// Load Moment.js
let momentScript = document.createElement("script");
momentScript.src = "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js";
document.body.appendChild(momentScript);
// Load FullCalendar CSS
let link = document.createElement("link");
link.href = "https://fullcalendar.io/releases/fullcalendar/3.9.0/fullcalendar.min.css";
link.rel = "stylesheet";
document.head.appendChild(link);
// Load FullCalendar JS after jQuery and Moment.js have loaded
Promise.all([
new Promise(resolve => jqueryScript.onload = resolve),
new Promise(resolve => momentScript.onload = resolve)
]).then(() => {
let script = document.createElement("script");
script.src = "https://fullcalendar.io/releases/fullcalendar/3.9.0/fullcalendar.min.js";
script.onload = function() {
// When FullCalendar JS is loaded, initialize the calendar
$('#calendar').fullCalendar({
defaultView: 'agendaWeek',
events: [
{
title: 'Math',
start: new Date().toISOString().slice(0,10) + ' 09:30:00',
end: new Date().toISOString().slice(0,10) + ' 10:30:00',
color: 'red'
},
{
title: 'Science',
start: new Date().toISOString().slice(0,10) + ' 11:00:00',
end: new Date().toISOString().slice(0,10) + ' 13:00:00',
color: 'blue'
}
]
});
};
document.body.appendChild(script);
});
''')
ui.run(title="Calendar") |
Beta Was this translation helpful? Give feedback.
-
I was trying to do something with this, and was wondering if you could help me debug this. def place_card():
card_element = ui.card().style("position: fixed; left:50%: top: 50%; z-index: 1000; height: 600px; width:1000px;").classes("my-calendar")
with card_element:
ui.add_body_html('''
<script src='https://cdn.jsdelivr.net/npm/[email protected]/index.global.min.js'></script>
<script>
document.addEventListener('DOMContentLoaded', function() {{
var calendarEvents = {events_json};
window.calendarInstance = new FullCalendar.Calendar(document.querySelector('.my-calendar'), {{
initialView: 'timeGridWeek',
slotMinTime: "05:00:00",
slotMaxTime: "22:00:00",
allDaySlot: false,
timeZone: 'local',
height: 'auto',
events:
calendarEvents
}});
window.calendarInstance.render();
}});
</script>
'''.format(events_json=json.dumps(globalState.events)))
ui.button("Close", on_click=lambda : card_element.delete()) |
Beta Was this translation helpful? Give feedback.
-
I found one, which uses MIT license |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I was working on a project wherein I needed a calendar.
I came across this, and was wondering if it was possible to implement this using nicegui, considering nicegui uses Quasar (iirc) under the hood.
https://github.com/quasarframework/quasar-ui-qcalendar/tree/dev
It is crucial to my project that I get some sort of calendar going.
Currently, I am trying to break this problem down into small parts - more specifically, I am now trying to display one singular event overtop a singular day, however I am having issues with that already due to some odd / funky positioning problems
Edit:
I am aware that there is already currently support for having a full month calendar, however I thought it would be interesting if we could expand this into weeks / days (realistically, weeks would just be repeated days).
Beta Was this translation helpful? Give feedback.
All reactions