Enaml

repository·main·Indexed 23 days ago

https://github.com/nucleic/enaml

A declarative, Pythonic programming language and framework for building professional-quality, cross-platform native GUIs. It features a constraints-based layout engine, integrated data modeling, and a reactive property system via the Object base class. The library includes a standard library (enaml.stdlib) for common UI tasks, docking layout management, and tools for live editing and AST traversal.

Tokens
39.9K
Snippets
111
Records
354
Agent score
81%

What's inside enaml

  1. Overview of Enaml

    main

    Enaml is a declarative programming language and framework designed for creating professional-quality user interfaces with minimal effort. It provides a Pythonic way to build native GUI applications across multiple platforms (Linux, Windows, MacOSX, Android, and iOS).

    Key features include:

    • Declarative Language: A Pythonic syntax for defining UI structure.
    • Widget Library: Dozens of out-of-the-box widgets built on Qt.
    • Constraints-based Layout: A layout engine built on Kiwi that uses symbolic constraints instead of pixel counting, allowing interfaces to adapt intelligently to different window sizes and platforms.
    • Data Model Integration: Built-in integration with Atom for automatic UI updates when the underlying data model changes.
    • Separation of Concerns: Encourages a clean separation between the model and the view while allowing Python code to be incorporated directly into the view layer.
  2. Use enaml.validator for data validation

    main

    The enaml.validator module provides a set of validator classes used to ensure data conforms to specific types or patterns. You can use these validators to validate input values during data processing or form handling.

    Available validator classes include:

    • Validator: The base class for all validators.
    • FloatValidator: Validates that a value is a floating-point number.
    • IntValidator: Validates that a value is an integer.
    • RegexValidator: Validates that a value matches a specific regular expression pattern.
  3. Enaml Advantages for Python Developers

    main

    Enaml provides several key advantages over traditional UI toolkits:

    • Ease of Prototyping: The syntax is designed for quick visualization of the UI. You can prototype visual aspects in isolation because data models do not need to be bound to the UI in advance.
    • Strict Model-View Separation: The MVC pattern is baked into the language. Data binding operators reduce the need for boilerplate controller logic and act as a sanity check for proper data modeling.
    • UI Toolkit Agnostic: Enaml acts as an abstraction layer. You code against declarative Enaml interfaces, and the framework translates them into procedural calls for the underlying rendering backend. This allows the same application code to run across different UI toolkits without modification.
  4. Use enaml.stdlib.fields for data validation

    main

    The enaml.stdlib.fields module provides specialized field classes used for defining and validating data types within Enaml applications. These fields are typically used in models or forms to ensure input adheres to specific constraints.

    Available field types include:

    • FloatField: Validates that input is a floating-point number.
    • IntField: Validates that input is an integer.
    • RegexField: Validates that input matches a specific regular expression pattern.
  5. Explore the enaml.stdlib modules

    main

    The enaml.stdlib package provides a collection of standard library modules for common UI and application tasks in Enaml. The available modules include:

    • dialog_buttons: Utilities for managing buttons within dialogs.
    • dock_area_styles: Styling options for dock areas.
    • fields: Standard field components.
    • mapped_view: Components for mapping data to views.
    • message_box: Standard message box implementations.
    • slider_transform: Transformations for slider components.
    • task_dialog: Specialized dialogs for managing tasks.
  6. Use enaml.layout.dock_layout for docking UI components

    main

    The enaml.layout.dock_layout module provides a suite of classes for managing complex UI layouts using docking and splitting logic. This includes managing how items are docked into bars, how they split existing areas, and how they float or tab within a layout structure.

    Key components include:

    • Layout Containers: DockLayout, DockBarLayout, AreaLayout, HSplitLayout, VSplitLayout, and SplitLayout for defining the structural organization of the UI.
    • Item Management: InsertItem, InsertBorderItem, InsertDockBarItem, InsertTab, RemoveItem, and RetractItem for manipulating the layout tree.
    • Floating and Tabbing: FloatItem, FloatArea, and TabLayout for managing non-docked or tabbed content.
    • Layout Logic: DockLayoutOp for operations and DockLayoutValidator for ensuring layout integrity.
  7. What is Enaml?

    main

    Enaml is a declarative extension to the Python language grammar. It allows developers to concisely define a hierarchical tree of objects that automatically react to changes in a data model.

    Unlike traditional imperative UI frameworks (like PyQt, PySide, or wxPython) where you must manually build object hierarchies and track state changes to update the UI, Enaml uses a declarative paradigm. In Enaml, you specify the UI structure and how visual elements bind to data; the framework then handles the synchronization between the data model and the view automatically.

  8. What is a Form widget and how does it work?

    main

    A Form is a subclass of Container designed to automatically lay out its children in two columns, ensuring widget edges are neatly aligned.

    Layout Behavior:

    • Children are arranged in two columns.
    • If the Form contains an odd number of children, the final child will automatically span both columns.
    • Typical Usage: Alternating Label and Field instances.
    • Constraints: Any child used within a Form must be 'constrainable'.
  9. Define linear relational constraints in Enaml

    main

    Enaml allows you to define constraints where the value of one property is a linear relation of another property. This is useful for creating complex layouts where positions or dimensions depend on the size of other elements.

    For example, you can make the horizontal position and width of a PushButton depend on the width of its parent Container, or make the vertical position of one element depend on the height or width of another.

  10. Use enaml.styling for component styling

    main

    The enaml.styling module provides the core abstractions for applying styles to Enaml components. It includes classes for defining styles, managing style caches, and applying styles via setters or stylesheets.

    Key classes include:

    • Style: Represents a specific set of style properties.
    • StyleSheet: A collection of styles used to style components.
    • Stylable: An interface or base class for objects that can be styled.
    • Setter: A mechanism for applying specific style values.
    • StyleCache: Manages the caching of styles for performance.
  11. Understand Enaml scoping rules

    main

    Enaml uses a combination of local, attribute, and dynamic scopes:

    1. Global Scope: Imports are global and accessible everywhere in the file.
    2. Local Namespace: Each top-level item has its own namespace. Expressions have a local namespace that is the union of block locals and the attribute namespace of the bound object (where self is implicit).
    3. Dynamic Scope: Expressions have a dynamic scope that is the chained union of all attribute namespaces in the ancestor tree (parents) of the widget. This allows a child widget to access attributes of its parents without manual propagation.

    Warning: Because of the implicit self, using the Python 3.8 walrus operator := inside a notification handler will create a local variable rather than modifying a widget attribute. To modify a widget attribute, use self.attribute_name.

    enamldef MyWindow(Window):
    
        attr a = 2
    
        attr b: str = ""
    
        a::
            b = 1
            self.b = "test"
    
        b::
            print(change)
            print(f"{a}")
  12. Use the DockArea widget for docking and undocking items

    main
    The DockArea widget provides a canvas for managing multiple DockItem components. It allows users to dock and undock items dynamically. A key feature of DockArea is its ability to save and restore the entire layout configuration using a layout object, which can be serialized via pickling.