diff --git a/.gitignore b/.gitignore index 7fae7a4..2cfd859 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ app.egg-info htmlcov .cache .venv +src/main.py tests/settings_from_unit_testing.json tests/TestDBEngineRoot tests/*.png diff --git a/README.md b/README.md index 1324e7e..6478086 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # MyFastHtml A utility library designed to simplify the development of FastHtml applications by providing: + - Predefined pages for common functionalities (e.g., authentication, user management). - A command management system to facilitate client-server interactions. - Helpers to create interactive controls more easily. @@ -10,7 +11,8 @@ A utility library designed to simplify the development of FastHtml applications ## Features -- **Dynamic HTML with HTMX**: Simplify dynamic interaction using attributes like `hx-post` and custom routes like `/commands`. +- **Dynamic HTML with HTMX**: Simplify dynamic interaction using attributes like `hx-post` and custom routes like + `/commands`. - **Command management**: Write server-side logic in Python while abstracting the complexities of HTMX. - **Control helpers**: Easily create reusable components like buttons. - **Predefined Pages (Roadmap)**: Include common pages like login, user management, and customizable dashboards. @@ -31,28 +33,59 @@ pip install myfasthtml ## Quick Start -Here’s a simple example of creating an **interactive button** linked to a command: +### FastHtml Application -### Example: Button with a Command +To create a simple FastHtml application, you can use the `create_app` function: ```python -from fasthtml.fastapp import fast_app +from fasthtml import serve +from fasthtml.components import * + +from myfasthtml.myfastapp import create_app + +app, rt = create_app(protect_routes=False) + + +@rt("/") +def get_homepage(): + return Div("Hello, FastHtml!") + + +if __name__ == "__main__": + serve(port=5002) + + +``` + +### Button with a Command + +```python +from fasthtml import serve + +from myfasthtml.controls.helpers import mk_button from myfasthtml.core.commands import Command -from myfasthtml.controls.button import mk_button +from myfasthtml.myfastapp import create_app + # Define a simple command action def say_hello(): - return "Hello, FastHtml!" + return "Hello, FastHtml!" + # Create the command hello_command = Command("say_hello", "Responds with a greeting", say_hello) -# Create the app and define a route with a button -app, rt = fast_app(default_hdrs=False) +# Create the app +app, rt = create_app(protect_routes=False) + @rt("/") def get_homepage(): - return mk_button("Click Me!", command=hello_command) + return mk_button("Click Me!", command=hello_command) + + +if __name__ == "__main__": + serve(port=5002) ``` - When the button is clicked, the `say_hello` command will be executed, and the server will return the response. @@ -63,31 +96,40 @@ def get_homepage(): ## Planned Features (Roadmap) ### Predefined Pages + The library will include predefined pages for: + - **Authentication**: Login, signup, password reset. - **User Management**: User profile and administration pages. - **Dashboard Templates**: Fully customizable dashboard components. - **Error Pages**: Detailed and styled error messages (e.g., 404, 500). ### State Persistence -Controls will have their state automatically synchronized between the client and the server. This feature is currently under construction. + +Controls will have their state automatically synchronized between the client and the server. This feature is currently +under construction. --- ## Advanced Features ### Command Management System -Commands allow you to simplify frontend/backend interaction. Instead of writing HTMX attributes manually, you can define Python methods and handle them as commands. + +Commands allow you to simplify frontend/backend interaction. Instead of writing HTMX attributes manually, you can define +Python methods and handle them as commands. #### Example + Here’s how `Command` simplifies dynamic interaction: ```python from myfasthtml.core.commands import Command + # Define a command def custom_action(data): - return f"Received: {data}" + return f"Received: {data}" + my_command = Command("custom", "Handles custom logic", custom_action) @@ -109,6 +151,7 @@ Use the `get_htmx_params()` method to directly integrate commands into HTML comp ## Contributing We welcome contributions! To get started: + 1. Fork the repository. 2. Create a feature branch. 3. Submit a pull request with clear descriptions of your changes. @@ -144,26 +187,32 @@ MyFastHtml ### Notable Classes and Methods #### 1. `Command` + Represents a backend action with server communication. + - **Attributes:** - - `id`: Unique identifier for the command. - - `name`: Command name (e.g., `say_hello`). - - `description`: Description of the command. + - `id`: Unique identifier for the command. + - `name`: Command name (e.g., `say_hello`). + - `description`: Description of the command. - **Method:** `get_htmx_params()` generates HTMX attributes. #### 2. `mk_button` + Simplifies the creation of interactive buttons linked to commands. + - **Arguments:** - - `element` (str): The label for the button. - - `command` (Command): Command associated with the button. - - `kwargs`: Additional button attributes. + - `element` (str): The label for the button. + - `command` (Command): Command associated with the button. + - `kwargs`: Additional button attributes. #### 3. `LoginPage` + Predefined login page that provides a UI template ready for integration. + - **Constructor Parameters:** - - `settings_manager`: Configuration/settings object. - - `error_message`: Optional error message to display. - - `success_message`: Optional success message to display. + - `settings_manager`: Configuration/settings object. + - `error_message`: Optional error message to display. + - `success_message`: Optional success message to display. --- @@ -177,7 +226,6 @@ Predefined login page that provides a UI template ready for integration. No custom exceptions defined yet. (Placeholder for future use.) - ## Relase History * 0.1.0 : First release diff --git a/src/myfasthtml/controls/button.py b/src/myfasthtml/controls/button.py deleted file mode 100644 index bbcc933..0000000 --- a/src/myfasthtml/controls/button.py +++ /dev/null @@ -1,11 +0,0 @@ -from fasthtml.components import * - -from myfasthtml.core.commands import Command - - -def mk_button(element, command: Command = None, **kwargs): - if command is None: - return Button(element, **kwargs) - - htmx = command.get_htmx_params() - return Button(element, **htmx, **kwargs) diff --git a/src/myfasthtml/core/commands.py b/src/myfasthtml/core/commands.py index 79254dc..54dcd7b 100644 --- a/src/myfasthtml/core/commands.py +++ b/src/myfasthtml/core/commands.py @@ -32,18 +32,24 @@ class BaseCommand: self.id = uuid.uuid4() self.name = name self.description = description + self.htmx_extra = {} # register the command CommandsManager.register(self) def get_htmx_params(self): - return { + return self.htmx_extra | { "hx-post": f"{ROUTE_ROOT}{Routes.Commands}", "hx-vals": f'{{"c_id": "{self.id}"}}', } def execute(self): raise NotImplementedError + + def htmx(self, target=None): + if target: + self.htmx_extra["hx-target"] = target + return self class Command(BaseCommand): diff --git a/src/myfasthtml/core/utils.py b/src/myfasthtml/core/utils.py index e2ef70f..0483ae4 100644 --- a/src/myfasthtml/core/utils.py +++ b/src/myfasthtml/core/utils.py @@ -17,3 +17,32 @@ def mount_if_not_exists(app, path: str, sub_app): if not is_mounted: app.mount(path, app=sub_app) + + +def merge_classes(*args): + all_elements = [] + for element in args: + if element is None or element == '': + continue + + if isinstance(element, (tuple, list, set)): + all_elements.extend(element) + + elif isinstance(element, dict): + if "cls" in element: + all_elements.append(element.pop("cls")) + elif "class" in element: + all_elements.append(element.pop("class")) + + elif isinstance(element, str): + all_elements.append(element) + + else: + raise ValueError(f"Cannot merge {element} of type {type(element)}") + + if all_elements: + # Remove duplicates while preserving order + unique_elements = list(dict.fromkeys(all_elements)) + return " ".join(unique_elements) + else: + return None diff --git a/src/myfasthtml/myfastapp.py b/src/myfasthtml/myfastapp.py index cccf3a1..9e35c66 100644 --- a/src/myfasthtml/myfastapp.py +++ b/src/myfasthtml/myfastapp.py @@ -8,6 +8,7 @@ from starlette.responses import Response from myfasthtml.auth.routes import setup_auth_routes from myfasthtml.auth.utils import create_auth_beforeware +from myfasthtml.core.commands import commands_app def get_asset_path(filename): @@ -25,7 +26,7 @@ def get_asset_content(filename): return get_asset_path(filename).read_text() -def create_app(daisyui: Optional[bool] = False, +def create_app(daisyui: Optional[bool] = True, protect_routes: Optional[bool] = True, mount_auth_app: Optional[bool] = False, **kwargs) -> Any: @@ -50,10 +51,10 @@ def create_app(daisyui: Optional[bool] = False, :return: A tuple containing the FastHtml application instance and the associated router. :rtype: Any """ - hdrs = [] + hdrs = [Link(href="/myfasthtml/myfasthtml.css", rel="stylesheet", type="text/css")] if daisyui: - hdrs = [ + hdrs += [ Link(href="/myfasthtml/daisyui-5.css", rel="stylesheet", type="text/css"), Link(href="/myfasthtml/daisyui-5-themes.css", rel="stylesheet", type="text/css"), Script(src="/myfasthtml/tailwindcss-browser@4.js"), @@ -84,6 +85,9 @@ def create_app(daisyui: Optional[bool] = False, # and put it back after the myfasthtml static files routes app.routes.append(static_route_exts_get) + # route the commands + app.mount("/myfasthtml", commands_app) + if mount_auth_app: # Setup authentication routes setup_auth_routes(app, rt) diff --git a/src/myfasthtml/core/matcher.py b/src/myfasthtml/test/matcher.py similarity index 96% rename from src/myfasthtml/core/matcher.py rename to src/myfasthtml/test/matcher.py index 67ce003..c340cc4 100644 --- a/src/myfasthtml/core/matcher.py +++ b/src/myfasthtml/test/matcher.py @@ -3,7 +3,7 @@ from dataclasses import dataclass from fastcore.basics import NotStr -from myfasthtml.core.testclient import MyFT +from myfasthtml.test.testclient import MyFT class Predicate: diff --git a/src/myfasthtml/core/testclient.py b/src/myfasthtml/test/testclient.py similarity index 59% rename from src/myfasthtml/core/testclient.py rename to src/myfasthtml/test/testclient.py index 9d94a0c..8aff99c 100644 --- a/src/myfasthtml/core/testclient.py +++ b/src/myfasthtml/test/testclient.py @@ -12,6 +12,14 @@ from starlette.testclient import TestClient from myfasthtml.core.commands import mount_commands +verbs = { + 'hx_get': 'GET', + 'hx_post': 'POST', + 'hx_put': 'PUT', + 'hx_delete': 'DELETE', + 'hx_patch': 'PATCH', +} + @dataclass class MyFT: @@ -29,7 +37,7 @@ class TestableElement: or verifying element properties. """ - def __init__(self, client, source): + def __init__(self, client, source, tag=None): """ Initialize a testable element. @@ -39,17 +47,34 @@ class TestableElement: """ self.client = client if isinstance(source, str): - self.html_fragment = source - tag = BeautifulSoup(source, 'html.parser').find() - self.ft = MyFT(tag.name, tag.attrs) + self.html_fragment = source.strip() elif isinstance(source, Tag): - self.html_fragment = str(source) - self.ft = MyFT(source.name, source.attrs) + self.html_fragment = str(source).strip() elif isinstance(source, FT): - self.ft = source self.html_fragment = to_xml(source).strip() else: raise ValueError(f"Invalid source '{source}' for TestableElement.") + + self.tag, self.element, self.my_ft = self._parse(tag, self.html_fragment) + self.fields_mapping = {} # link between the input label and the input name + self.fields = {} # Values of the fields {name: value} + self.select_fields = {} # list of possible options for 'select' input fields + + self._update_fields_mapping() + self._update_fields() + + def fill(self, **kwargs): + """ + Fill the form with the given data. + + Args: + **kwargs: Field names and their values to fill in the form. + """ + for name, value in kwargs.items(): + field_name = self._translate(name) + if field_name not in self.fields: + raise ValueError(f"Invalid field name '{name}'.") + self.fields[self._translate(name)] = value def click(self): """Click the element (to be implemented).""" @@ -59,6 +84,26 @@ class TestableElement: """Check if element matches given FastHTML element (to be implemented).""" pass + def _translate(self, field): + """ + Translate a given field using a predefined mapping. If the field is not found + in the mapping, the original field is returned unmodified. + + :param field: The field name to be translated. + :type field: str + :return: The translated field name if present in the mapping, or the original + field name if no mapping exists for it. + :rtype: str + """ + return self.fields_mapping.get(field, field) + + def _support_htmx(self): + """Check if the element supports HTMX.""" + return ('hx_get' in self.my_ft.attrs or + 'hx-get' in self.my_ft.attrs or + 'hx_post' in self.my_ft.attrs or + 'hx-post' in self.my_ft.attrs) + def _send_htmx_request(self, json_data: dict | None = None, data: dict | None = None) -> Response: """ Simulates an HTMX request in Python for unit testing. @@ -83,16 +128,13 @@ class TestableElement: method = "GET" # HTMX defaults to GET if not specified url = None - verbs = { - 'hx_get': 'GET', - 'hx_post': 'POST', - 'hx_put': 'PUT', - 'hx_delete': 'DELETE', - 'hx_patch': 'PATCH', - } + if data is not None: + headers['Content-Type'] = 'application/x-www-form-urlencoded' + elif json_data is not None: + headers['Content-Type'] = 'application/json' # .props contains the kwargs passed to the object (e.g., hx_post="/url") - element_attrs = self.ft.attrs or {} + element_attrs = self.my_ft.attrs or {} # Build the attributes for key, value in element_attrs.items(): @@ -124,180 +166,13 @@ class TestableElement: # Sanity check if url is None: raise ValueError( - f"The <{self.ft.tag}> element has no HTMX verb attribute " + f"The <{self.my_ft.tag}> element has no HTMX verb attribute " "(e.g., hx_get, hx_post) to define a URL." ) # Send the request return self.client.send_request(method, url, headers=headers, data=data, json_data=json_data) - def _support_htmx(self): - """Check if the element supports HTMX.""" - return ('hx_get' in self.ft.attrs or - 'hx-get' in self.ft.attrs or - 'hx_post' in self.ft.attrs or - 'hx-post' in self.ft.attrs) - - -class TestableForm(TestableElement): - """ - Represents an HTML form that can be filled and submitted in tests. - """ - - def __init__(self, client, source): - """ - Initialize a testable form. - - Args: - client: The MyTestClient instance. - source: The source HTML string containing a form. - """ - super().__init__(client, source) - self.form = BeautifulSoup(self.html_fragment, 'html.parser').find('form') - self.fields_mapping = {} # link between the input label and the input name - self.fields = {} # field name; field value - self.select_fields = {} # list of possible options for 'select' input fields - - self._update_fields_mapping() - self.update_fields() - - def update_fields(self): - """ - Update the fields dictionary with current form values and their proper types. - - This method processes all input and select elements in the form: - - Determines the appropriate Python type (str, int, float, bool) based on - the HTML input type attribute and/or the value itself - - For select elements, populates self.select_fields with available options - - Stores the final typed values in self.fields - - Type conversion priority: - 1. HTML type attribute (checkbox → bool, number → int/float, etc.) - 2. Value analysis fallback for ambiguous types (text/hidden/absent type) - """ - self.fields = {} - self.select_fields = {} - - # Process input fields - for input_field in self.form.find_all('input'): - name = input_field.get('name') - if not name: - continue - - input_type = input_field.get('type', 'text').lower() - raw_value = input_field.get('value', '') - - # Type conversion based on input type - if input_type == 'checkbox': - # Checkbox: bool based on 'checked' attribute - self.fields[name] = input_field.has_attr('checked') - - elif input_type == 'radio': - # Radio: str value (only if checked) - if input_field.has_attr('checked'): - self.fields[name] = raw_value - elif name not in self.fields: - # If no radio is checked yet, don't set a default - pass - - elif input_type == 'number': - # Number: int or float based on value - self.fields[name] = self._convert_number(raw_value) - - else: - # Other types (text, hidden, email, password, etc.): analyze value - self.fields[name] = self._convert_value(raw_value) - - # Process select fields - for select_field in self.form.find_all('select'): - name = select_field.get('name') - if not name: - continue - - # Extract all options - options = [] - selected_value = None - - for option in select_field.find_all('option'): - option_value = option.get('value', option.get_text(strip=True)) - option_text = option.get_text(strip=True) - - options.append({ - 'value': option_value, - 'text': option_text - }) - - # Track selected option - if option.has_attr('selected'): - selected_value = option_value - - # Store options list - self.select_fields[name] = options - - # Store selected value (or first option if none selected) - if selected_value is not None: - self.fields[name] = selected_value - elif options: - self.fields[name] = options[0]['value'] - - def fill(self, **kwargs): - """ - Fill the form with the given data. - - Args: - **kwargs: Field names and their values to fill in the form. - """ - for name, value in kwargs.items(): - field_name = self.translate(name) - if field_name not in self.fields: - raise ValueError(f"Invalid field name '{name}'.") - self.fields[self.translate(name)] = value - - def submit(self): - """ - Submit the form. - - This method handles both HTMX-enabled forms and classic HTML form submissions: - - If the form supports HTMX (has hx_post, hx_get, etc.), uses HTMX request - - Otherwise, simulates a classic browser form submission using the form's - action and method attributes - - Returns: - The response from the form submission. - - Raises: - ValueError: If the form has no action attribute for classic submission. - """ - # Check if the form supports HTMX - if self._support_htmx(): - return self._send_htmx_request(data=self.fields) - - # Classic form submission - action = self.form.get('action') - if not action or action.strip() == '': - raise ValueError( - "The form has no 'action' attribute. " - "Cannot submit a classic form without a target URL." - ) - - method = self.form.get('method', 'post').upper() - - # Prepare headers for classic form submission - headers = { - "Content-Type": "application/x-www-form-urlencoded" - } - - # Send the request via the client - return self.client.send_request( - method=method, - url=action, - headers=headers, - data=self.fields - ) - - def translate(self, field): - return self.fields_mapping.get(field, field) - def _update_fields_mapping(self): """ Build a mapping between label text and input field names. @@ -319,16 +194,16 @@ class TestableForm(TestableElement): unnamed_counter = 0 # Get all inputs in the form - all_inputs = self.form.find_all('input') + all_inputs = self.element.find_all('input') # Priority 1 & 2: Explicit association (for/id) and implicit (nested) - for label in self.form.find_all('label'): + for label in self.element.find_all('label'): label_text = label.get_text(strip=True) # Check for explicit association via 'for' attribute label_for = label.get('for') if label_for: - input_field = self.form.find('input', id=label_for) + input_field = self.element.find('input', id=label_for) if input_field: input_name = self._get_input_identifier(input_field, unnamed_counter) if input_name.startswith('unnamed_'): @@ -348,7 +223,7 @@ class TestableForm(TestableElement): continue # Priority 3 & 4: Parent-level associations - for label in self.form.find_all('label'): + for label in self.element.find_all('label'): label_text = label.get_text(strip=True) # Skip if this label was already processed @@ -391,6 +266,89 @@ class TestableForm(TestableElement): unnamed_counter += 1 self.fields_mapping[input_name] = input_name + def _update_fields(self): + """ + Update the fields dictionary with current form values and their proper types. + + This method processes all input and select elements in the form: + - Determines the appropriate Python type (str, int, float, bool) based on + the HTML input type attribute and/or the value itself + - For select elements, populates self.select_fields with available options + - Stores the final typed values in self.fields + + Type conversion priority: + 1. HTML type attribute (checkbox → bool, number → int/float, etc.) + 2. Value analysis fallback for ambiguous types (text/hidden/absent type) + """ + self.fields = {} + self.select_fields = {} + + # Process input fields + for input_field in self.element.find_all('input'): + name = input_field.get('name') + if not name: + continue + + input_type = input_field.get('type', 'text').lower() + raw_value = input_field.get('value', '') + + # Type conversion based on input type + if input_type == 'checkbox': + # Checkbox: bool based on 'checked' attribute + self.fields[name] = input_field.has_attr('checked') + + elif input_type == 'radio': + # Radio: str value (only if checked) + if input_field.has_attr('checked'): + self.fields[name] = raw_value + elif name not in self.fields: + # If no radio is checked yet, don't set a default + pass + + elif input_type == 'number': + # Number: int or float based on value + self.fields[name] = self._convert_number(raw_value) + + else: + # Other types (text, hidden, email, password, etc.): analyze value + self.fields[name] = self._convert_value(raw_value) + + # Process select fields + for select_field in self.element.find_all('select'): + name = select_field.get('name') + if not name: + continue + + # Extract all options + options = [] + selected_value = None + + for option in select_field.find_all('option'): + option_value = option.get('value', option.get_text(strip=True)) + option_text = option.get_text(strip=True) + + options.append({ + 'value': option_value, + 'text': option_text + }) + + # Track selected option + if option.has_attr('selected'): + selected_value = option_value + + # Store options list + self.select_fields[name] = options + + # Store selected value (or first option if none selected) + if selected_value is not None: + self.fields[name] = selected_value + elif options: + self.fields[name] = options[0]['value'] + + def _get_my_ft(self, element: Tag): + _inner = element.find(self.tag) if self.tag and self.tag != element.name else element + return MyFT(_inner.name, _inner.attrs) + @staticmethod def _get_input_identifier(input_field, counter): """ @@ -473,8 +431,458 @@ class TestableForm(TestableElement): # Default to string return value + + @staticmethod + def _get_element(html_fragment: str): + html_fragment = html_fragment.strip() + if (not html_fragment.startswith('