Context Workflows¶
Contexts in TaskWarrior allow you to focus on specific subsets of tasks by applying filters automatically.
Context Definition and Application¶
Creating Contexts¶
from taskwarrior import TaskWarrior
tw = TaskWarrior()
# Create contexts for different workflows
tw.define_context(ContextDTO(name="work", read_filter="project:work or +urgent", write_filter="project:work or +urgent"))
tw.define_context(ContextDTO(name="home", read_filter="project:home or project:personal", write_filter="project:home or project:personal"))
tw.define_context(ContextDTO(name="errands", read_filter="+errand or +shopping", write_filter="+errand or +shopping"))
Applying Contexts¶
# Switch to work context
tw.apply_context("work")
# Now get_tasks() only returns work-related tasks
work_tasks = tw.get_tasks()
print(f"Found {len(work_tasks)} work tasks")
Checking Current Context¶
current = tw.get_current_context()
if current:
print(f"Current context: {current}")
else:
print("No context active")
Listing Contexts¶
contexts = tw.get_contexts()
for ctx in contexts:
print(f"{ctx.name}: read={ctx.read_filter} write={ctx.write_filter}")
Context Switching Patterns¶
Workflow-Based Context Switching¶
def switch_to_work_context(tw):
"""Switch to work context if not already active"""
if not tw.has_context("work"):
tw.define_context(ContextDTO(name="work", read_filter="project:work or +urgent", write_filter="project:work or +urgent"))
tw.apply_context("work")
def switch_to_home_context(tw):
"""Switch to home context if not already active"""
if not tw.has_context("home"):
tw.define_context(ContextDTO(name="home", read_filter="project:home or project:personal", write_filter="project:home or project:personal"))
tw.apply_context("home")
Context-Specific Operations¶
# Work context operations
tw.apply_context("work")
work_tasks = tw.get_tasks()
for task in work_tasks:
print(f"Work: {task.description}")
# Home context operations
tw.apply_context("home")
home_tasks = tw.get_tasks()
for task in home_tasks:
print(f"Home: {task.description}")
Predefined Context Examples¶
Focus Context¶
# Create a focus context for high-priority items
if not tw.has_context("focus"):
tw.define_context(ContextDTO(name="focus", read_filter="priority:H or +urgent", write_filter="priority:H or +urgent"))
# Morning routine: check high-priority items
tw.apply_context("focus")
urgent_tasks = tw.get_tasks()
print(f"🔥 {len(urgent_tasks)} urgent tasks today")
Time-Based Contexts¶
# Morning context (tasks due today)
tw.define_context(ContextDTO(name="morning", read_filter="due.today or status:pending", write_filter="due.today or status:pending"))
# Evening context (tasks due tomorrow)
tw.define_context(ContextDTO(name="evening", read_filter="due.tomorrow or status:pending", write_filter="due.tomorrow or status:pending"))
Context Best Practices¶
Keep Filters Simple¶
# Good - simple and fast
tw.define_context(ContextDTO(name="work", read_filter="project:work", write_filter="project:work"))
# Avoid - complex filters that slow down queries
tw.define_context(ContextDTO(name="work", read_filter="project:work and (priority:H or +urgent) and not +done", write_filter="project:work and (priority:H or +urgent) and not +done"))
Use Meaningful Names¶
# Clear context names
tw.define_context(ContextDTO(name="work", read_filter="project:work or +urgent", write_filter="project:work or +urgent"))
tw.define_context(ContextDTO(name="home", read_filter="project:home or project:personal", write_filter="project:home or project:personal"))
# Less clear context names
tw.define_context(ContextDTO(name="ctx1", read_filter="project:work or +urgent", write_filter="project:work or +urgent"))
tw.define_context(ContextDTO(name="ctx2", read_filter="project:home or project:personal", write_filter="project:home or project:personal"))
Combine with Projects¶
# Project-specific contexts work well
tw.define_context(ContextDTO(name="work.meetings", read_filter="project:work.meetings", write_filter="project:work.meetings"))
tw.define_context(ContextDTO(name="work.research", read_filter="project:work.research", write_filter="project:work.research"))
Test Before Applying¶
# Test filter first before applying context
test_tasks = tw.get_tasks("project:work or +urgent")
print(f"Test found {len(test_tasks)} tasks")
# Then apply context
tw.define_context(ContextDTO(name="work", read_filter="project:work or +urgent", write_filter="project:work or +urgent"))
tw.apply_context("work")
Advanced Filtering with Contexts¶
Nested Contexts¶
# Create a nested context for specific projects within work
tw.define_context(ContextDTO(name="work.project1", read_filter="project:work.project1", write_filter="project:work.project1"))
tw.define_context(ContextDTO(name="work.project2", read_filter="project:work.project2", write_filter="project:work.project2"))
# Switch between specific project contexts
tw.apply_context("work.project1")
project1_tasks = tw.get_tasks()
Context Composition¶
# Create a context that combines multiple filters
tw.define_context(ContextDTO(name="urgent-work", read_filter="project:work and priority:H", write_filter="project:work and priority:H"))
tw.define_context(ContextDTO(name="pending-urgent", read_filter="status:pending and +urgent", write_filter="status:pending and +urgent"))
# Use composition for complex workflows
tw.apply_context("urgent-work")
urgent_work_tasks = tw.get_tasks()
Related Documentation¶
- Task Management Patterns - Common patterns for task management
- UDA Implementation - User Defined Attributes best practices
- Task Dependencies - Complex task relationships