123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- """Import all classes and functions the end user will need to make an app.
- Anything imported here will be available in the default Reflex import as `rx.*`.
- To signal to typecheckers that something should be reexported,
- we use the Flask "import name as name" syntax.
- """
- from __future__ import annotations
- import importlib
- from typing import Type
- from reflex.page import page as page
- from reflex.utils import console
- from reflex.utils.format import to_snake_case
- _ALL_COMPONENTS = [
- "Accordion",
- "AccordionButton",
- "AccordionIcon",
- "AccordionItem",
- "AccordionPanel",
- "Alert",
- "AlertDescription",
- "AlertDialog",
- "AlertDialogBody",
- "AlertDialogContent",
- "AlertDialogFooter",
- "AlertDialogHeader",
- "AlertDialogOverlay",
- "AlertIcon",
- "AlertTitle",
- "AspectRatio",
- "Audio",
- "Avatar",
- "AvatarBadge",
- "AvatarGroup",
- "Badge",
- "Box",
- "Breadcrumb",
- "BreadcrumbItem",
- "BreadcrumbLink",
- "BreadcrumbSeparator",
- "Button",
- "ButtonGroup",
- "Card",
- "CardBody",
- "CardFooter",
- "CardHeader",
- "Center",
- "Checkbox",
- "CheckboxGroup",
- "CircularProgress",
- "CircularProgressLabel",
- "Circle",
- "Code",
- "CodeBlock",
- "Collapse",
- "ColorModeButton",
- "ColorModeIcon",
- "ColorModeSwitch",
- "Component",
- "Cond",
- "ConnectionBanner",
- "ConnectionModal",
- "Container",
- "DataTable",
- "DataEditor",
- "DataEditorTheme",
- "DatePicker",
- "DateTimePicker",
- "DebounceInput",
- "Divider",
- "Drawer",
- "DrawerBody",
- "DrawerCloseButton",
- "DrawerContent",
- "DrawerFooter",
- "DrawerHeader",
- "DrawerOverlay",
- "Editable",
- "EditableInput",
- "EditablePreview",
- "EditableTextarea",
- "Editor",
- "Email",
- "Fade",
- "Flex",
- "Foreach",
- "Form",
- "FormControl",
- "FormErrorMessage",
- "FormHelperText",
- "FormLabel",
- "Fragment",
- "Grid",
- "GridItem",
- "Heading",
- "Highlight",
- "Hstack",
- "Html",
- "Icon",
- "IconButton",
- "Image",
- "Input",
- "InputGroup",
- "InputLeftAddon",
- "InputLeftElement",
- "InputRightAddon",
- "InputRightElement",
- "Kbd",
- "Link",
- "LinkBox",
- "LinkOverlay",
- "List",
- "ListItem",
- "Markdown",
- "Menu",
- "MenuButton",
- "MenuDivider",
- "MenuGroup",
- "MenuItem",
- "MenuItemOption",
- "MenuList",
- "MenuOptionGroup",
- "Modal",
- "ModalBody",
- "ModalCloseButton",
- "ModalContent",
- "ModalFooter",
- "ModalHeader",
- "ModalOverlay",
- "Moment",
- "MultiSelect",
- "MultiSelectOption",
- "NextLink",
- "NumberDecrementStepper",
- "NumberIncrementStepper",
- "NumberInput",
- "NumberInputField",
- "NumberInputStepper",
- "Option",
- "OrderedList",
- "Password",
- "PinInput",
- "PinInputField",
- "Plotly",
- "Popover",
- "PopoverAnchor",
- "PopoverArrow",
- "PopoverBody",
- "PopoverCloseButton",
- "PopoverContent",
- "PopoverFooter",
- "PopoverHeader",
- "PopoverTrigger",
- "Progress",
- "Radio",
- "RadioGroup",
- "RangeSlider",
- "RangeSliderFilledTrack",
- "RangeSliderThumb",
- "RangeSliderTrack",
- "ResponsiveGrid",
- "ScaleFade",
- "Script",
- "Select",
- "Skeleton",
- "SkeletonCircle",
- "SkeletonText",
- "Slide",
- "SlideFade",
- "Slider",
- "SliderFilledTrack",
- "SliderMark",
- "SliderThumb",
- "SliderTrack",
- "Spacer",
- "Span",
- "Spinner",
- "Square",
- "Stack",
- "Stat",
- "StatArrow",
- "StatGroup",
- "StatHelpText",
- "StatLabel",
- "StatNumber",
- "Step",
- "StepDescription",
- "StepIcon",
- "StepIndicator",
- "StepNumber",
- "StepSeparator",
- "StepStatus",
- "StepTitle",
- "Stepper",
- "Switch",
- "Tab",
- "TabList",
- "TabPanel",
- "TabPanels",
- "Table",
- "TableCaption",
- "TableContainer",
- "Tabs",
- "Tag",
- "TagCloseButton",
- "TagLabel",
- "TagLeftIcon",
- "TagRightIcon",
- "Tbody",
- "Td",
- "Text",
- "TextArea",
- "Tfoot",
- "Th",
- "Thead",
- "Tooltip",
- "Tr",
- "UnorderedList",
- "Upload",
- "Video",
- "VisuallyHidden",
- "Vstack",
- "Wrap",
- "WrapItem",
- ]
- _ALL_COMPONENTS += [to_snake_case(component) for component in _ALL_COMPONENTS]
- _ALL_COMPONENTS += [
- "cancel_upload",
- "components",
- "color_mode_cond",
- "desktop_only",
- "mobile_only",
- "tablet_only",
- "mobile_and_tablet",
- "tablet_and_desktop",
- "selected_files",
- "clear_selected_files",
- "EditorButtonList",
- "EditorOptions",
- "NoSSRComponent",
- ]
- _MAPPING = {
- "reflex.admin": ["admin", "AdminDash"],
- "reflex.app": ["app", "App", "UploadFile"],
- "reflex.base": ["base", "Base"],
- "reflex.compiler": ["compiler"],
- "reflex.compiler.utils": ["get_asset_path"],
- "reflex.components": _ALL_COMPONENTS + ["chakra", "next"],
- "reflex.components.component": ["memo"],
- "reflex.components.graphing": ["recharts"],
- "reflex.components.datadisplay.moment": ["MomentDelta"],
- "reflex.config": ["config", "Config", "DBConfig"],
- "reflex.constants": ["constants", "Env"],
- "reflex.components.el": ["el"],
- "reflex.event": [
- "event",
- "EventChain",
- "background",
- "call_script",
- "clear_local_storage",
- "console_log",
- "download",
- "prevent_default",
- "redirect",
- "remove_cookie",
- "remove_local_storage",
- "set_clipboard",
- "set_focus",
- "set_value",
- "stop_propagation",
- "upload_files",
- "window_alert",
- ],
- "reflex.middleware": ["middleware", "Middleware"],
- "reflex.model": ["model", "session", "Model"],
- "reflex.page": ["page"],
- "reflex.route": ["route"],
- "reflex.state": ["state", "var", "Cookie", "LocalStorage", "State"],
- "reflex.style": ["style", "color_mode", "toggle_color_mode"],
- "reflex.testing": ["testing"],
- "reflex.utils": ["utils"],
- "reflex.vars": ["vars", "cached_var", "Var"],
- }
- def _reverse_mapping(mapping: dict[str, list]) -> dict[str, str]:
- """Reverse the mapping used to lazy loading, and check for conflicting name.
- Args:
- mapping: The mapping to reverse.
- Returns:
- The reversed mapping.
- """
- reversed_mapping = {}
- for key, values in mapping.items():
- for value in values:
- if value not in reversed_mapping:
- reversed_mapping[value] = key
- else:
- console.warn(
- f"Key {value} is present multiple times in the imports _MAPPING: {key} / {reversed_mapping[value]}"
- )
- return reversed_mapping
- # _MAPPING = {value: key for key, values in _MAPPING.items() for value in values}
- _MAPPING = _reverse_mapping(_MAPPING)
- def _removeprefix(text, prefix):
- return text[text.startswith(prefix) and len(prefix) :]
- __all__ = [_removeprefix(mod, "reflex.") for mod in _MAPPING]
- def __getattr__(name: str) -> Type:
- """Lazy load all modules.
- Args:
- name: name of the module to load.
- Returns:
- The module or the attribute of the module.
- Raises:
- AttributeError: If the module or the attribute does not exist.
- """
- try:
- # Check for import of a module that is not in the mapping.
- if name not in _MAPPING:
- # If the name does not start with reflex, add it.
- if not name.startswith("reflex") and name != "__all__":
- name = f"reflex.{name}"
- return importlib.import_module(name)
- # Import the module.
- module = importlib.import_module(_MAPPING[name])
- # Get the attribute from the module if the name is not the module itself.
- return (
- getattr(module, name) if name != _MAPPING[name].rsplit(".")[-1] else module
- )
- except ModuleNotFoundError:
- raise AttributeError(f"module 'reflex' has no attribute {name}") from None
|