Running an event with limited capacity? You want people to sign up without the risk of them showing up only to find out the event is fully booked. Group scheduling is the ideal solution to manage event participation, and the Nylas Python SDK makes it possible.
All you need to do is create an event, specify its capacity, and inform potential participants about the available spots for them to join.
Is your system ready?
If you already have the Nylas Python SDK installed and your Python environment configured, feel free to continue with the blog.
Before proceeding further, let’s take a look at what our application will look like:
We have the title, date, and time of the event. Additionally, we can see the number of available spots, and there are fields to enter our name and email address.
After pressing submit, you’ll be registered, and a confirmation message will be sent:
Additionally, you’ll receive an invitation in your mailbox:
When registering, it’s crucial to complete all fields. Let’s ensure we have some form validation:
When there are no more available spots, the registration will fail:
With a confirmation message:
Installing the Flask package
As we aim to create a web application, our best option is to use Flask, one of the most popular micro-frameworks in the Python world. To install it, run the following commands:
$ pip3 install Flask
$ pip3 install Flask-session
Also, we need to install an additional package called Ice Cream. This package will help us get a better debug print output.
$ pip3 install icecream
Once installed, we’re ready to go.
Creating an Event Generator
As we need to create the event, we’re going to create a file called EventGenerator.py:
# Load your env variables
from dotenv import load_dotenv
load_dotenv()
# Import your dependencies
from nylas import APIClient
import os
import datetime
from datetime import date
from icecream import ic
# Initialize your Nylas API client
nylas = APIClient(
os.environ.get("CLIENT_ID"),
os.environ.get("CLIENT_SECRET"),
os.environ.get("ACCESS_TOKEN"),
)
# Get today's date
today = date.today()
# Schedule start time for today at
START_TIME = int(
datetime.datetime(today.year, today.month, today.day, 11, 00, 0).strftime("%s")
)
# Schedule end time for today at
END_TIME = int(
datetime.datetime(today.year, today.month, today.day, 12, 0, 0).strftime("%s")
)
# Create the event holder
event = nylas.events.create()
# Define the event properties
event.title = "Zumba Class with Nyla"
event.location = "Nylas' Office"
event.when = {"start_time": START_TIME, "end_time": END_TIME}
event.calendar_id = os.environ.get("CALENDAR_ID")
event.capacity = 3
event.save(notify_participants=True)
if event.id:
print("Event created successfully")
ic(event)
else:
print("There was an error creating the event")
We can execute this file by typing the following in the terminal:
$ python3 EventGenerator.py
Here is the output:
We must copy the ID of the event as we’ll need it. Open up your .env file and add the following:
We need a confirmation page; let’s call it GroupConfirmation.html:
{% extends 'base.html' %}
{% block content %}
<div class="bg-[#315acb] border-green-600 border-b p-4 m-4 rounded grid place-items-center">
<h1 class="text-3xl">Thanks {{name}} - {{email}}</h1>
<p class="font-semibold">You have successfully registered for {{title}}</p>
<p class="font-semibold">You can now go <a href="{{url_for('index')}}" class="text-green-600">back</a></p>
</div>
{% endblock %}
Finally, let’s create GroupFull.html to inform people that the event is full:
{% extends 'base.html' %}
{% block content %}
<div class="bg-[#315acb] border-green-600 border-b p-4 m-4 rounded grid place-items-center">
<h1 class="text-3xl">Sorry {{name}} - {{email}}</h1>
<p class="font-semibold">Sadly, there's no space left for you to join {{title}}</p>
<p class="font-semibold">You can now go <a href="{{url_for('index')}}" class="text-green-600">back</a></p>
</div>
{% endblock %}
The final step is to add an image to the static folder. Here’s the one we’re using:
Running our Group Scheduling application
To run our application, simply type the following in the terminal window:
$ python3 GroupScheduling.py
The application runs on port 5000 of localhost, so open your favourite browser and navigate to the following address:
http://localhost:5000
Control group participation with Python, is a great way to manage our scheduling events.
If you want to learn more about our Calendar APIs, check out the Nylas documentation Calendar API Overview.