How to use the magic variable __CATEGORIES__ from Python?

This documentation:
Shows this example to query for only events of a certain category:

events = flood(query_bucket(find_bucket(“aw-watcher-window_”)));
not_afk = flood(query_bucket(find_bucket(“aw-watcher-afk_”)));
not_afk = filter_keyvals(not_afk, “status”, [“not-afk”]);
events = filter_period_intersect(events, not_afk);
events = categorize(events, __CATEGORIES__);
events = filter_keyvals(events, “$category”, [[“Work”]]);
RETURN = sort_by_duration(events);

Incidentally this example doesn’t work for any events that are inside a subcategory, like “[ “Work”, “Programming”, “ActivityWatch” ]”, but that’s beside the point.

I can’t seem to be able to do this with a Python based query, as the __CATEGORIES__ magic variable seems to only work from the online query builder.
I get the following error:

Error message received: {‘type’: ‘QueryInterpretException’, ‘message’: “Tried to reference variable ‘__CATEGORIES__’ which is not defined”}

Is there any way that I can do this purely from Python?

The __CATEGORIES__ magic variable is just a substitution done in the web UI, and is not otherwise available in queries.

You can run the query in the web UI query explorer, inspect the network request, and copy the categories from the request data.

Since we recently added server-side settings in a recent beta (v0.12.3b12), we could make this easier in the future, adding something like get_setting("categories") that could be used directly in the query. But we’re not there quite yet.