Assign Groups Demo¶
This notebook demonstrates how to load group assignments from a CSV or DataFrame using AssignGroup.
Load groups from a DataFrame¶
In [1]:
Copied!
import pandas as pd
df = pd.DataFrame({
"group_name": ["Team_Alpha", "Team_Alpha", "Team_Alpha", "Team_Beta", "Team_Beta", "Team_Beta"],
"student_id": ["alice", "bob", "carol", "dave", "eve", "frank"],
})
df
import pandas as pd
df = pd.DataFrame({
"group_name": ["Team_Alpha", "Team_Alpha", "Team_Alpha", "Team_Beta", "Team_Beta", "Team_Beta"],
"student_id": ["alice", "bob", "carol", "dave", "eve", "frank"],
})
df
Out[1]:
| group_name | student_id | |
|---|---|---|
| 0 | Team_Alpha | alice |
| 1 | Team_Alpha | bob |
| 2 | Team_Alpha | carol |
| 3 | Team_Beta | dave |
| 4 | Team_Beta | eve |
| 5 | Team_Beta | frank |
Use AssignGroup to load and inspect groups¶
We use unittest.mock.MagicMock here to simulate the Canvas and GitHub services without real API credentials.
In [2]:
Copied!
from unittest.mock import MagicMock
from CanvasGroupy import AssignGroup
# Mock the Canvas and GitHub services for demo purposes
mock_cg = MagicMock()
mock_ghg = MagicMock()
ag = AssignGroup(ghg=mock_ghg, cg=mock_cg)
ag.load_groups(df)
print("Loaded groups:")
for name, members in ag.groups.items():
print(f" {name}: {members}")
from unittest.mock import MagicMock
from CanvasGroupy import AssignGroup
# Mock the Canvas and GitHub services for demo purposes
mock_cg = MagicMock()
mock_ghg = MagicMock()
ag = AssignGroup(ghg=mock_ghg, cg=mock_cg)
ag.load_groups(df)
print("Loaded groups:")
for name, members in ag.groups.items():
print(f" {name}: {members}")
Loaded groups: Team_Alpha: ['alice', 'bob', 'carol'] Team_Beta: ['dave', 'eve', 'frank']
Load groups from a CSV file¶
You can also pass a file path to a CSV with the same group_name and student_id columns:
In [3]:
Copied!
import tempfile, os
csv_content = """group_name,student_id
Team_Alpha,alice
Team_Alpha,bob
Team_Beta,carol
Team_Beta,dave"""
print("CSV content:")
print(csv_content)
# Write to a temp file and load
with tempfile.NamedTemporaryFile(mode="w", suffix=".csv", delete=False) as f:
f.write(csv_content)
csv_path = f.name
ag2 = AssignGroup(ghg=mock_ghg, cg=mock_cg)
ag2.load_groups(csv_path)
print("\nLoaded from CSV:")
for name, members in ag2.groups.items():
print(f" {name}: {members}")
os.unlink(csv_path)
import tempfile, os
csv_content = """group_name,student_id
Team_Alpha,alice
Team_Alpha,bob
Team_Beta,carol
Team_Beta,dave"""
print("CSV content:")
print(csv_content)
# Write to a temp file and load
with tempfile.NamedTemporaryFile(mode="w", suffix=".csv", delete=False) as f:
f.write(csv_content)
csv_path = f.name
ag2 = AssignGroup(ghg=mock_ghg, cg=mock_cg)
ag2.load_groups(csv_path)
print("\nLoaded from CSV:")
for name, members in ag2.groups.items():
print(f" {name}: {members}")
os.unlink(csv_path)
CSV content: group_name,student_id Team_Alpha,alice Team_Alpha,bob Team_Beta,carol Team_Beta,dave Loaded from CSV: Team_Alpha: ['alice', 'bob'] Team_Beta: ['carol', 'dave']
Next steps¶
Once groups are loaded, you can:
- Call
ag.create_canvas_group(in_group_category="...")to create groups on Canvas - Call
ag.create_github_group(username_quiz_id=...)to create GitHub repos for each group
See the Canvas Groups and GitHub Repos tutorials for details.