Conventions¶
Three rules hold across the library, so a behavior learned in one place works the same way in the next.
The Three-Tier Precedence Model¶
Where you can choose a behavior in more than one place, the later place wins:
- The class attribute, the default for every view of the class.
- A method you override, deciding for one view while it runs.
- An argument at the call, which wins over both.
from cascadeui import StatefulLayoutView, button_row, card
class Draft(StatefulLayoutView):
exit_policy = "delete" # (1)!
# ... text, unsaved, and a save() callback, as the panel keeps them
def build_ui(self):
self.clear_items()
self.add_item(card("## Draft", self.text, button_row({"Save": self.save})))
async def exit(self, delete_message=None):
if delete_message is None and self.unsaved: # (2)!
delete_message = False
await super().exit(delete_message=delete_message)
# ... later, with draft a Draft that was sent
await draft.exit(delete_message=True) # (3)!
- The class: closing deletes the message.
- The override: unsaved work stays on screen with its controls disabled. It changes only what the caller
left as
None, so the third tier still wins. - The call: deleted, saved or not.
A *_message attribute and its on_* hook are the first two tiers: the default on_unauthorized()
sends unauthorized_message, and an override decides for itself. set_class_attribute(name, value)
changes a class attribute for one view.
Sync or Async, Your Choice¶
Write async def when the body awaits something and a plain def when it does not:
class Standings(StatefulLayoutView):
async def on_load(self): # awaits, so async
self.rows = await db.fetch_top(10)
def build_ui(self): # composes, so not
self.clear_items()
self.add_item(card(*self.rows))
Every place the library runs your code takes either shape: view hooks, component callbacks, store subscribers
and store.on() hooks, reducers, middleware, validators, the pattern builders,
from_cursor()'s fetch function, and migrators. The library checks what your function returns, so an
object with an async __call__ and a functools.partial of an async function work too.
The places below are the exceptions. They run where nothing can wait, so each needs a plain def. An async
one raises TypeError where you hand it to the library, naming the place and the fix:
- Selectors:
state_selector, a subscriber'sselector=, and@computed(its selector and its function). - What a draw reads: a V2 paginator's page function, a wizard step's
condition,step_indicator_label,Collapsible'srevealandsummary, a function given aschoice_row(selected=),get_files(), the leaderboard'sformat_rank,format_name,format_stats, andformat_accessory, and the roles panel'sformat_category_title,format_category_hint,format_button_label,format_button_style, andformat_button_emoji. - What a pattern builds without waiting: every pattern's
build_extra_items(), the menu'sbuild_header(),build_footer(),build_category_item(),build_menu_card(),build_category_button(), and V1build_embed(), a tab view'sbuild_tab_rows(), the wizard'sbuild_progress_header(), the V1 paginator'spage_kwargs(), and the roles panel'sbuild_category_card(). The leaderboard waits for its ownbuild_header()andbuild_footer(), so those take either shape. - Inside a reducer: a function given to
dispatch_scoped(). restore_on_dropped_render(rebuild=).
build_ui takes either shape on every view but three. MenuLayoutView, RolesLayoutView, and
DisplayLayoutView build their tree in __init__, which cannot await, so an async def build_ui on one
of them raises TypeError when it is built, pointing at on_load() for the work that awaits.
Mappings and Pairs¶
These take a sequence of (key, value) pairs as well as a dict, and any other mapping (an OrderedDict,
a UserDict, a defaultdict), in the order given:
from cascadeui import key_value
key_value({"Wins": 12, "Losses": 3})
key_value([("Wins", 12), ("Losses", 3)]) # the same lines
| Takes pairs | Two pairs sharing a key |
|---|---|
key_value(data=), stats_card(stats=) |
two lines |
button_row(buttons=), tab_nav(tabs=), choice_row(options=) |
two entries |
Theme(styles=), TabView(tabs=), TabLayoutView(tabs=), RoleCategory(roles=) |
ValueError naming the key |
The builders draw one entry per pair, so a label read from data can repeat. A theme's style, a tab's name,
or a role button's label must mean one thing, where a dict would keep only the last. Anything else, text included, raises
TypeError naming the builder and the argument.
Extension Strategies¶
How the library adds to discord.py (subclasses, builders, wrappers, patterns, and inputs) has a page of its own: Extension Strategies.
Related¶
- Lifecycle and Closing:
exit_policyand whatexit()does with the message - Who Can Click:
unauthorized_messageandon_unauthorized() - Class Settings: changing a class attribute for one view, and the checks on it