mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-19 18:27:29 -05:00
* add JSProperty to pydom so it can be used to create descriptors mapping to specific JS attributes * add pyweb.ui * fix pyweb imports * fix link and a elements and add a script element * fix imports and add initialization to load resources to shoelace module * new pyweb.ui test folder * remove comments * add Icon to shoelace components * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * use html property rather than accessing _js directly * add markdown suppport * move examples section out of stdlib pyweb to examples.py in the demo itself * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * simplify demo code * improve docstrings * precommit fixes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * simplify code for main page * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add load_resources to markdown * add showlace extra style dynamically * precommit * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add gallery files * add global attributes and dynamic property assignment * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add shoelace radio component (#1961) * add shoelace radio component * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * fix type that lead using the the JSDescriptor directly instead of the factory method * add missing marked dependency * refactor gallary to simplify codebase * precommig lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add text attribute to pydom Elements * add global JS attributes to elements and improve demos * lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix image instantiation on card since the API has changed * add attributes to all classes in elements * lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * change example creation functions to take the label and the object directly * fix input name clashing with input keyword * refactor examples to better simplify and automate * fix div clashing names * fix demo left menu width * simplify base elements demo by moving all the examples to the examples module * rename Grid to grid to align to other elements * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * reorg classes order in elements.py and add missing elements to examples * fix issue related to now importing div from pyweb.ui * improve demo * link and fix spelling typo * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add a bunch more elements (#1966) * Add copy button * Add skeleton and spinner * Add Switch * Add text area * Add more elements * More styling to each component * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Fabio Pliger <fpliger@users.noreply.github.com> * Add radio group (#1963) * add radio group * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Fabio Pliger <fpliger@users.noreply.github.com> * Small tweaks to main demo page (#1962) * Small tweaks to main demo page * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Fabio Pliger <fpliger@users.noreply.github.com> * fix post merge issues * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixed issues with renaming Grid to grid, after we merged * lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add multple HTML elements in alphabetical order from abbr to em * fix attributes of some of the elements added in the previous commit and add embed * fix embed attributes and add fieldset * add figcation, figure and form. Also fix ordering of definitoin of img and input_ * add style and lint * wrap up adding all major html elements * fix test failing due to different error message from fake server compared to a real test server * change default docstring associated with all classes dynamically patched * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add pyweb tests * lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add global attributes and change abbr test * fix abbr to inherit from TextElementBase * add address test and improve error messaging when ElementBase gets a bad input as style * change test helper function to be more flexible on attributes and manage content vs non content based elements. Also adds area tests * add test for more elements up to caption * fix canvas and caption as elements that have content and fix name typo on figcaption * fix another typo on figcaption * minor fixes and complete tests for all elements * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * adapt shoelace to latest upates in ui.elements * fix issue with example code not showing created button * move global attributes to base class * replace all the calls to _add_js_properties with defining attributes directly in the classes * finish moving all properties manually on each class * remove _add_js_properties * replace JSProperty with js_property * replace JSProperty with js_property * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixed merge conflicts * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove js_property and just use JSProperty with name and allow_nones as arguments * fix bug around Element not being able to map global attributes in subclasses * remove js_property and fix references * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * precommit * precommit again * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * enable pyweb on micropython * switch examples to micropython * fix pydom issue with micropython, created by the monkeypatch around JsProxy * print micropython version on micropython pydom example * lint and remove of textwrap in stdlib for micropython compatibility * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * added msising attributes on the option Element. Tests are all passing now * fix tests * lint * temp ugly fix for micropython, using the when decorator with a function without arguments * small fixes and improvements to examples * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix examples and broken link from the removal of markdown and shoelace from stdlib * lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * dynamically change the server address in tests * use the right element in test_a * lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * skipping test_audio in CI due to different behavior of fake_server vs a real server, that runs in local tests * add conditional expected_missing_file_errors property to manage different behaviour between local tests and CI (due to fake_server) * lint * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Askat <aaskat@users.noreply.github.com> Co-authored-by: Fábio Rosado <hello@fabiorosado.dev> Co-authored-by: Andrea Giammarchi <andrea.giammarchi@gmail.com>
181 lines
4.9 KiB
Python
181 lines
4.9 KiB
Python
try:
|
|
from textwrap import dedent
|
|
except ImportError:
|
|
dedent = lambda x: x
|
|
|
|
import inspect
|
|
|
|
import shoelace
|
|
import styles
|
|
import tictactoe
|
|
from markdown import markdown
|
|
from pyscript import when, window
|
|
from pyweb import pydom
|
|
from pyweb.ui import elements as el
|
|
|
|
MAIN_PAGE_MARKDOWN = dedent(
|
|
"""
|
|
This gallery is a collection of demos using the PyWeb.UI library. There are meant
|
|
to be examples of how to use the library to create GUI applications using Python
|
|
only.
|
|
|
|
## How to use the gallery
|
|
|
|
Simply click on the demo you want to see and the details will appear on the right
|
|
"""
|
|
)
|
|
|
|
# First thing we do is to load all the external resources we need
|
|
shoelace.load_resources()
|
|
|
|
|
|
def add_demo(demo_name, demo_creator_cb, parent_div, source=None):
|
|
"""Create a link to a component and add it to the left panel.
|
|
|
|
Args:
|
|
component (str): The name of the component to add.
|
|
|
|
Returns:
|
|
the component created
|
|
|
|
"""
|
|
# Create the component link element
|
|
div = el.div(el.a(demo_name, href="#"), style=styles.STYLE_LEFT_PANEL_LINKS)
|
|
|
|
# Create a handler that opens the component details when the link is clicked
|
|
@when("click", div)
|
|
def _change():
|
|
if source:
|
|
demo_div = el.grid("50% 50%")
|
|
demo_div.append(demo_creator_cb())
|
|
widget_code = markdown(dedent(f"""```python\n{source}\n```"""))
|
|
demo_div.append(el.div(widget_code, style=styles.STYLE_CODE_BLOCK))
|
|
else:
|
|
demo_div = demo_creator_cb()
|
|
demo_div.style["margin"] = "20px"
|
|
write_to_main(demo_div)
|
|
window.hljs.highlightAll()
|
|
|
|
# Add the new link element to the parent div (left panel)
|
|
parent_div.append(div)
|
|
return div
|
|
|
|
|
|
def create_main_area():
|
|
"""Create the main area of the right side of page, with the description of the
|
|
demo itself and how to use it.
|
|
|
|
Returns:
|
|
the main area
|
|
|
|
"""
|
|
return el.div(
|
|
[
|
|
el.h1("PyWeb UI Gallery", style={"text-align": "center"}),
|
|
markdown(MAIN_PAGE_MARKDOWN),
|
|
]
|
|
)
|
|
|
|
|
|
def create_markdown_app():
|
|
"""Create the basic components page.
|
|
|
|
Returns:
|
|
the main area
|
|
|
|
"""
|
|
translate_button = shoelace.Button("Convert", variant="primary")
|
|
markdown_txt_area = shoelace.TextArea(label="Use this to write your Markdown")
|
|
result_div = el.div(style=styles.STYLE_MARKDOWN_RESULT)
|
|
|
|
@when("click", translate_button)
|
|
def translate_markdown():
|
|
result_div.html = markdown(markdown_txt_area.value).html
|
|
|
|
return el.div(
|
|
[
|
|
el.h2("Markdown"),
|
|
markdown_txt_area,
|
|
translate_button,
|
|
result_div,
|
|
],
|
|
style={"margin": "20px"},
|
|
)
|
|
|
|
|
|
# ********** MAIN PANEL **********
|
|
main_area = create_main_area()
|
|
|
|
|
|
def write_to_main(content):
|
|
main_area.html = ""
|
|
main_area.append(content)
|
|
|
|
|
|
def restore_home():
|
|
write_to_main(create_main_area())
|
|
|
|
|
|
def create_new_section(title, parent_div):
|
|
basic_components_text = el.h3(
|
|
title, style={"text-align": "left", "margin": "20px auto 0"}
|
|
)
|
|
parent_div.append(basic_components_text)
|
|
parent_div.append(
|
|
shoelace.Divider(style={"margin-top": "5px", "margin-bottom": "30px"})
|
|
)
|
|
return basic_components_text
|
|
|
|
|
|
# ********** LEFT PANEL **********
|
|
left_panel_title = el.h1("PyWeb.UI", style=styles.STYLE_LEFT_PANEL_TITLE)
|
|
left_div = el.div(
|
|
[
|
|
left_panel_title,
|
|
shoelace.Divider(style={"margin-bottom": "30px"}),
|
|
el.h3("Demos", style=styles.STYLE_LEFT_PANEL_TITLE),
|
|
]
|
|
)
|
|
|
|
# Let's map the creation of the main area to when the user clocks on "Components"
|
|
when("click", left_panel_title)(restore_home)
|
|
|
|
# ------ ADD DEMOS ------
|
|
markdown_source = """
|
|
translate_button = shoelace.Button("Convert", variant="primary")
|
|
markdown_txt_area = shoelace.TextArea(label="Markdown",
|
|
help_text="Write your Mardown here and press convert to see the result",
|
|
)
|
|
result_div = el.div(style=styles.STYLE_MARKDOWN_RESULT)
|
|
@when("click", translate_button)
|
|
def translate_markdown():
|
|
result_div.html = markdown(markdown_txt_area.value).html
|
|
|
|
el.div([
|
|
el.h2("Markdown"),
|
|
markdown_txt_area,
|
|
translate_button,
|
|
result_div,
|
|
])
|
|
"""
|
|
add_demo("Markdown", create_markdown_app, left_div, source=markdown_source)
|
|
add_demo(
|
|
"Tic Tac Toe",
|
|
tictactoe.create_tic_tac_toe,
|
|
left_div,
|
|
source=inspect.getsource(tictactoe),
|
|
)
|
|
|
|
left_div.append(shoelace.Divider(style={"margin-top": "5px", "margin-bottom": "30px"}))
|
|
left_div.append(el.a("Examples", href="/test/ui/", style={"text-align": "left"}))
|
|
|
|
# ********** CREATE ALL THE LAYOUT **********
|
|
grid = el.grid("minmax(100px, 200px) 20px auto", style={"min-height": "100%"})
|
|
grid.append(left_div)
|
|
grid.append(shoelace.Divider(vertical=True))
|
|
grid.append(main_area)
|
|
|
|
pydom.body.append(grid)
|
|
pydom.body.append(el.a("Back to the main page", href="/test/ui/", target="_blank"))
|
|
pydom.body.append(el.a("Hidden!!!", href="/test/ui/", target="_blank", hidden=True))
|