Skip to content

Commit

Permalink
Add default parameter in some methods + update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mubarakalmehairbi committed Aug 2, 2023
1 parent 06709ec commit f688bdf
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ Python code:

.. code-block:: python
import sys
sys.path.append("..")
import os
from toui import Website, Page
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ToUI
====

Version: v3.1.0
Version: v3.2.0

.. include:: ../README.md
:parser: myst_parser.sphinx_
Expand Down
2 changes: 0 additions & 2 deletions examples/advanced_example_5_toui_with_sql_user_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
Python code:
"""
import sys
sys.path.append("..")
import os
from toui import Website, Page

Expand Down
2 changes: 1 addition & 1 deletion toui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from .structure import ToUIBlueprint
from . import exceptions

__version__ = "v3.1.0"
__version__ = "v3.2.0"
91 changes: 74 additions & 17 deletions toui/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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':
"""
Expand Down Expand Up @@ -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.
Expand All @@ -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]
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions toui/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand Down

0 comments on commit f688bdf

Please sign in to comment.