From f688bdf7bc96d11087ba1ca56bd992d87a247e34 Mon Sep 17 00:00:00 2001 From: mubarakalmehairbi Date: Wed, 2 Aug 2023 14:47:11 +0400 Subject: [PATCH] Add `default` parameter in some methods + update docs --- ..._example_5_toui_with_sql_user_database.rst | 2 - docs/index.rst | 2 +- ...d_example_5_toui_with_sql_user_database.py | 2 - toui/__init__.py | 2 +- toui/elements.py | 91 +++++++++++++++---- toui/pages.py | 5 + 6 files changed, 81 insertions(+), 23 deletions(-) diff --git a/docs/Examples.advanced_example_5_toui_with_sql_user_database.rst b/docs/Examples.advanced_example_5_toui_with_sql_user_database.rst index 30a88a0..2e81fe8 100644 --- a/docs/Examples.advanced_example_5_toui_with_sql_user_database.rst +++ b/docs/Examples.advanced_example_5_toui_with_sql_user_database.rst @@ -32,8 +32,6 @@ Python code: .. code-block:: python - import sys - sys.path.append("..") import os from toui import Website, Page diff --git a/docs/index.rst b/docs/index.rst index b3bf396..21fbb72 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1,7 +1,7 @@ ToUI ==== -Version: v3.1.0 +Version: v3.2.0 .. include:: ../README.md :parser: myst_parser.sphinx_ diff --git a/examples/advanced_example_5_toui_with_sql_user_database.py b/examples/advanced_example_5_toui_with_sql_user_database.py index 7422908..c219ebe 100644 --- a/examples/advanced_example_5_toui_with_sql_user_database.py +++ b/examples/advanced_example_5_toui_with_sql_user_database.py @@ -30,8 +30,6 @@ Python code: """ -import sys -sys.path.append("..") import os from toui import Website, Page diff --git a/toui/__init__.py b/toui/__init__.py index 7040ebf..18c055f 100644 --- a/toui/__init__.py +++ b/toui/__init__.py @@ -4,4 +4,4 @@ from .structure import ToUIBlueprint from . import exceptions -__version__ = "v3.1.0" +__version__ = "v3.2.0" diff --git a/toui/elements.py b/toui/elements.py index 5c6c8aa..ab4d78c 100644 --- a/toui/elements.py +++ b/toui/elements.py @@ -335,7 +335,7 @@ def get_unique_selector(self): return ' > '.join(path) - def get_attr(self, name): + def get_attr(self, name, default=None): """ Gets the value of an HTML element attribute. @@ -344,18 +344,24 @@ def get_attr(self, name): name: str The name of the attribute. + default: Any, default=None + The value to return if the attribute does not exist. + Returns ------- str If the attribute exists. - None - If the attribute does not exist. + None, Any + ``None`` will be returned the attribute does not exist. However, if a `default` value is specified, + it will be returned instead. """ value = self._element.attrs.get(name) if type(value) == list: value = ' '.join(value) + if value is None: + value = default return value @_ElementSignal() @@ -405,20 +411,26 @@ def del_attr(self, name): if self.has_attr(name): del self._element.attrs[name] - def get_id(self): + def get_id(self, default=None): """ Gets the ``id`` attribute of the HTML element. + Parameters + ---------- + default: Any, default=None + The value to return if the attribute does not exist. + Returns ------- str If the attribute exists. - None - If the attribute does not exist. + None, Any + Retuns ``None`` if the attribute does not exist. However, if a `default` value is specified, + it will be returned instead. """ - return self.get_attr("id") + return self.get_attr("id", default=default) def set_id(self, value): """ @@ -432,20 +444,26 @@ def set_id(self, value): """ self.set_attr("id", value) - def get_value(self): + def get_value(self, default=None): """ Gets the ``value`` attribute of the HTML element. + Parameters + ---------- + default: Any, default=None + The value to return if the attribute does not exist. + Returns ------- str If the attribute exists. - None - If the attribute does not exist. + None, Any + Retuns ``None`` if the attribute does not exist. However, if a `default` value is specified, + it will be returned instead. """ - return self.get_attr("value") + return self.get_attr("value", default=default) def get_selected(self) -> 'Element': """ @@ -553,7 +571,7 @@ def add_content(self, content): content = BeautifulSoup(content, features="html.parser") self._element.append(content) - def get_style_property(self, property): + def get_style_property(self, property, default=None): """ Gets the value of a CSS property inside the ``style`` attribute. @@ -562,13 +580,21 @@ def get_style_property(self, property): property: str The name of the property + default: Any, default=None + The value to return if the property does not exist or the ``style`` attribute does not exist. + Returns ------- str + If the property exists. + + None, Any + Retuns ``None`` if the property does not exist or the ``style`` attribute does not exist. + However, if a `default` value is specified, it will be returned instead. """ if not self.has_attr("style"): - return + return default style = self.get_attr('style') parser = tinycss.make_parser("page3") declarations = parser.parse_style_attr(style)[0] @@ -578,6 +604,7 @@ def get_style_property(self, property): for v in declaration.value: property_value += v.as_css() return property_value + return default def set_style_property(self, property, value): """ @@ -614,16 +641,26 @@ def set_style_property(self, property, value): new_style += f"{property}: {value};" self.set_attr(name="style", value=new_style) - def get_width_property(self): + def get_width_property(self, default=None): """ Gets the value of the CSS property `width` inside the ``style`` attribute. + Parameters + ---------- + default: Any, default=None + The value to return if the property does not exist or the ``style`` attribute does not exist. + Returns ------- str + If the property exists. + + None, Any + Retuns ``None`` if the property does not exist or the ``style`` attribute does not exist. + However, if a `default` value is specified, it will be returned instead. """ - return self.get_style_property("width") + return self.get_style_property("width", default=default) def set_width_property(self, value): """ @@ -636,16 +673,26 @@ def set_width_property(self, value): """ self.set_style_property("width", value) - def get_height_property(self): + def get_height_property(self, default=None): """ Gets the value of the CSS property `height` inside the ``style`` attribute. + Parameters + ---------- + default: Any, default=None + The value to return if the property does not exist or the ``style`` attribute does not exist. + Returns ------- str + If the property exists. + + None, Any + Retuns ``None`` if the property does not exist or the ``style`` attribute does not exist. + However, if a `default` value is specified, it will be returned instead. """ - return self.get_style_property("height") + return self.get_style_property("height", default=default) def set_height_property(self, value): """ @@ -673,6 +720,11 @@ def on(self, event, func_or_name, *func_args, quotes=True, return_itself=False): If you want to add JavaScript code instead of a single function, use `Element.set_attr` method instead. + Warning + ------- + If you added a Python function, users might be able to call this function from the client-side. Choose wisely + the functions you add. + Parameters ---------- event: str @@ -751,6 +803,11 @@ def onclick(self, func_or_name, *func_args, quotes=True, return_itself=False): If you want to add JavaScript code instead of a single function, use `Element.set_attr` method instead. + Warning + ------- + If you added a Python function, users might be able to call this function from the client-side. Choose wisely + the functions you add. + Parameters ---------- func_or_name: Callable or str diff --git a/toui/pages.py b/toui/pages.py index dbb0a35..caab959 100644 --- a/toui/pages.py +++ b/toui/pages.py @@ -419,6 +419,11 @@ def add_function(self, func): """ Adds a function to the `Page`. This function can be called from an HTML element. + Warning + ------- + If you added a Python function, users might be able to call this function from the client-side. Choose wisely + the functions you add. + Examples --------