pythonnative 0.4.0__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. pythonnative/__init__.py +45 -65
  2. pythonnative/cli/pn.py +15 -14
  3. pythonnative/components.py +241 -0
  4. pythonnative/element.py +47 -0
  5. pythonnative/native_views.py +800 -0
  6. pythonnative/page.py +319 -247
  7. pythonnative/reconciler.py +129 -0
  8. pythonnative/templates/android_template/app/src/main/java/com/pythonnative/android_template/PageFragment.kt +2 -1
  9. pythonnative/utils.py +21 -29
  10. {pythonnative-0.4.0.dist-info → pythonnative-0.5.0.dist-info}/METADATA +35 -17
  11. {pythonnative-0.4.0.dist-info → pythonnative-0.5.0.dist-info}/RECORD +15 -35
  12. pythonnative/activity_indicator_view.py +0 -71
  13. pythonnative/button.py +0 -113
  14. pythonnative/date_picker.py +0 -76
  15. pythonnative/image_view.py +0 -78
  16. pythonnative/label.py +0 -133
  17. pythonnative/list_view.py +0 -76
  18. pythonnative/material_activity_indicator_view.py +0 -71
  19. pythonnative/material_button.py +0 -69
  20. pythonnative/material_date_picker.py +0 -87
  21. pythonnative/material_progress_view.py +0 -70
  22. pythonnative/material_search_bar.py +0 -69
  23. pythonnative/material_switch.py +0 -69
  24. pythonnative/material_time_picker.py +0 -76
  25. pythonnative/picker_view.py +0 -69
  26. pythonnative/progress_view.py +0 -70
  27. pythonnative/scroll_view.py +0 -101
  28. pythonnative/search_bar.py +0 -69
  29. pythonnative/stack_view.py +0 -199
  30. pythonnative/switch.py +0 -68
  31. pythonnative/text_field.py +0 -132
  32. pythonnative/text_view.py +0 -135
  33. pythonnative/time_picker.py +0 -77
  34. pythonnative/view.py +0 -173
  35. pythonnative/web_view.py +0 -60
  36. {pythonnative-0.4.0.dist-info → pythonnative-0.5.0.dist-info}/WHEEL +0 -0
  37. {pythonnative-0.4.0.dist-info → pythonnative-0.5.0.dist-info}/entry_points.txt +0 -0
  38. {pythonnative-0.4.0.dist-info → pythonnative-0.5.0.dist-info}/licenses/LICENSE +0 -0
  39. {pythonnative-0.4.0.dist-info → pythonnative-0.5.0.dist-info}/top_level.txt +0 -0
pythonnative/__init__.py CHANGED
@@ -1,74 +1,54 @@
1
- from importlib import import_module
2
- from typing import Any, Dict
1
+ """PythonNative declarative native UI for Android and iOS.
3
2
 
4
- __version__ = "0.4.0"
3
+ Public API::
4
+
5
+ import pythonnative as pn
6
+
7
+ class MainPage(pn.Page):
8
+ def __init__(self, native_instance):
9
+ super().__init__(native_instance)
10
+ self.state = {"count": 0}
11
+
12
+ def render(self):
13
+ return pn.Column(
14
+ pn.Text(f"Count: {self.state['count']}", font_size=24),
15
+ pn.Button("Increment", on_click=lambda: self.set_state(count=self.state["count"] + 1)),
16
+ spacing=12,
17
+ )
18
+ """
19
+
20
+ __version__ = "0.5.0"
21
+
22
+ from .components import (
23
+ ActivityIndicator,
24
+ Button,
25
+ Column,
26
+ Image,
27
+ ProgressBar,
28
+ Row,
29
+ ScrollView,
30
+ Spacer,
31
+ Switch,
32
+ Text,
33
+ TextInput,
34
+ WebView,
35
+ )
36
+ from .element import Element
37
+ from .page import Page
5
38
 
6
39
  __all__ = [
7
- "ActivityIndicatorView",
40
+ "ActivityIndicator",
8
41
  "Button",
9
- "DatePicker",
10
- "ImageView",
11
- "Label",
12
- "ListView",
13
- "MaterialActivityIndicatorView",
14
- "MaterialButton",
15
- "MaterialDatePicker",
16
- "MaterialProgressView",
17
- "MaterialSearchBar",
18
- "MaterialSwitch",
19
- "MaterialTimePicker",
20
- "MaterialBottomNavigationView",
21
- "MaterialToolbar",
42
+ "Column",
43
+ "Element",
44
+ "Image",
22
45
  "Page",
23
- "PickerView",
24
- "ProgressView",
46
+ "ProgressBar",
47
+ "Row",
25
48
  "ScrollView",
26
- "SearchBar",
27
- "StackView",
49
+ "Spacer",
28
50
  "Switch",
29
- "TextField",
30
- "TextView",
31
- "TimePicker",
51
+ "Text",
52
+ "TextInput",
32
53
  "WebView",
33
54
  ]
34
-
35
- _NAME_TO_MODULE: Dict[str, str] = {
36
- "ActivityIndicatorView": ".activity_indicator_view",
37
- "Button": ".button",
38
- "DatePicker": ".date_picker",
39
- "ImageView": ".image_view",
40
- "Label": ".label",
41
- "ListView": ".list_view",
42
- "MaterialActivityIndicatorView": ".material_activity_indicator_view",
43
- "MaterialButton": ".material_button",
44
- "MaterialDatePicker": ".material_date_picker",
45
- "MaterialProgressView": ".material_progress_view",
46
- "MaterialSearchBar": ".material_search_bar",
47
- "MaterialSwitch": ".material_switch",
48
- "MaterialTimePicker": ".material_time_picker",
49
- "MaterialBottomNavigationView": ".material_bottom_navigation_view",
50
- "MaterialToolbar": ".material_toolbar",
51
- "Page": ".page",
52
- "PickerView": ".picker_view",
53
- "ProgressView": ".progress_view",
54
- "ScrollView": ".scroll_view",
55
- "SearchBar": ".search_bar",
56
- "StackView": ".stack_view",
57
- "Switch": ".switch",
58
- "TextField": ".text_field",
59
- "TextView": ".text_view",
60
- "TimePicker": ".time_picker",
61
- "WebView": ".web_view",
62
- }
63
-
64
-
65
- def __getattr__(name: str) -> Any:
66
- module_path = _NAME_TO_MODULE.get(name)
67
- if not module_path:
68
- raise AttributeError(f"module 'pythonnative' has no attribute {name!r}")
69
- module = import_module(module_path, package=__name__)
70
- return getattr(module, name)
71
-
72
-
73
- def __dir__() -> Any:
74
- return sorted(list(globals().keys()) + __all__)
pythonnative/cli/pn.py CHANGED
@@ -41,7 +41,6 @@ def init_project(args: argparse.Namespace) -> None:
41
41
 
42
42
  os.makedirs(app_dir, exist_ok=True)
43
43
 
44
- # Minimal hello world app scaffold (no bootstrap function; host instantiates Page directly)
45
44
  main_page_py = os.path.join(app_dir, "main_page.py")
46
45
  if not os.path.exists(main_page_py) or args.force:
47
46
  with open(main_page_py, "w", encoding="utf-8") as f:
@@ -52,20 +51,22 @@ def init_project(args: argparse.Namespace) -> None:
52
51
  class MainPage(pn.Page):
53
52
  def __init__(self, native_instance):
54
53
  super().__init__(native_instance)
55
-
56
- def on_create(self):
57
- super().on_create()
58
- stack = (
59
- pn.StackView()
60
- .set_axis("vertical")
61
- .set_spacing(12)
62
- .set_alignment("fill")
63
- .set_padding(all=16)
54
+ self.state = {"count": 0}
55
+
56
+ def increment(self):
57
+ self.set_state(count=self.state["count"] + 1)
58
+
59
+ def render(self):
60
+ return pn.ScrollView(
61
+ pn.Column(
62
+ pn.Text("Hello from PythonNative!", font_size=24, bold=True),
63
+ pn.Text(f"Tapped {self.state['count']} times"),
64
+ pn.Button("Tap me", on_click=self.increment),
65
+ spacing=12,
66
+ padding=16,
67
+ alignment="fill",
68
+ )
64
69
  )
65
- stack.add_view(pn.Label("Hello from PythonNative!").set_text_size(18))
66
- button = pn.Button("Tap me").set_on_click(lambda: print("Button clicked"))
67
- stack.add_view(button)
68
- self.set_root_view(stack.wrap_in_scroll())
69
70
  """
70
71
  )
71
72
 
@@ -0,0 +1,241 @@
1
+ """Built-in element-creating functions for declarative UI composition.
2
+
3
+ Each function returns an :class:`Element` describing a native UI widget.
4
+ These are pure data — no native views are created until the reconciler
5
+ mounts the element tree.
6
+
7
+ Naming follows React Native conventions:
8
+
9
+ - ``Text`` (was *Label*)
10
+ - ``Button``
11
+ - ``Column`` / ``Row`` (was *StackView* vertical/horizontal)
12
+ - ``ScrollView``
13
+ - ``TextInput`` (was *TextField*)
14
+ - ``Image`` (was *ImageView*)
15
+ - ``Switch``
16
+ - ``ProgressBar`` (was *ProgressView*)
17
+ - ``ActivityIndicator`` (was *ActivityIndicatorView*)
18
+ - ``WebView``
19
+ - ``Spacer`` (new)
20
+ """
21
+
22
+ from typing import Any, Callable, Dict, Optional, Union
23
+
24
+ from .element import Element
25
+
26
+
27
+ def _filter_none(**kwargs: Any) -> Dict[str, Any]:
28
+ """Return *kwargs* with ``None``-valued entries removed."""
29
+ return {k: v for k, v in kwargs.items() if v is not None}
30
+
31
+
32
+ # ---------------------------------------------------------------------------
33
+ # Leaf components
34
+ # ---------------------------------------------------------------------------
35
+
36
+
37
+ def Text(
38
+ text: str = "",
39
+ *,
40
+ font_size: Optional[float] = None,
41
+ color: Optional[str] = None,
42
+ bold: bool = False,
43
+ text_align: Optional[str] = None,
44
+ background_color: Optional[str] = None,
45
+ max_lines: Optional[int] = None,
46
+ key: Optional[str] = None,
47
+ ) -> Element:
48
+ """Display text."""
49
+ props = _filter_none(
50
+ text=text,
51
+ font_size=font_size,
52
+ color=color,
53
+ bold=bold or None,
54
+ text_align=text_align,
55
+ background_color=background_color,
56
+ max_lines=max_lines,
57
+ )
58
+ return Element("Text", props, [], key=key)
59
+
60
+
61
+ def Button(
62
+ title: str = "",
63
+ *,
64
+ on_click: Optional[Callable[[], None]] = None,
65
+ color: Optional[str] = None,
66
+ background_color: Optional[str] = None,
67
+ font_size: Optional[float] = None,
68
+ enabled: bool = True,
69
+ key: Optional[str] = None,
70
+ ) -> Element:
71
+ """Create a tappable button."""
72
+ props: Dict[str, Any] = {"title": title}
73
+ if on_click is not None:
74
+ props["on_click"] = on_click
75
+ if color is not None:
76
+ props["color"] = color
77
+ if background_color is not None:
78
+ props["background_color"] = background_color
79
+ if font_size is not None:
80
+ props["font_size"] = font_size
81
+ if not enabled:
82
+ props["enabled"] = False
83
+ return Element("Button", props, [], key=key)
84
+
85
+
86
+ def TextInput(
87
+ *,
88
+ value: str = "",
89
+ placeholder: str = "",
90
+ on_change: Optional[Callable[[str], None]] = None,
91
+ secure: bool = False,
92
+ font_size: Optional[float] = None,
93
+ color: Optional[str] = None,
94
+ background_color: Optional[str] = None,
95
+ key: Optional[str] = None,
96
+ ) -> Element:
97
+ """Create a single-line text entry field."""
98
+ props: Dict[str, Any] = {"value": value}
99
+ if placeholder:
100
+ props["placeholder"] = placeholder
101
+ if on_change is not None:
102
+ props["on_change"] = on_change
103
+ if secure:
104
+ props["secure"] = True
105
+ if font_size is not None:
106
+ props["font_size"] = font_size
107
+ if color is not None:
108
+ props["color"] = color
109
+ if background_color is not None:
110
+ props["background_color"] = background_color
111
+ return Element("TextInput", props, [], key=key)
112
+
113
+
114
+ def Image(
115
+ source: str = "",
116
+ *,
117
+ width: Optional[float] = None,
118
+ height: Optional[float] = None,
119
+ scale_type: Optional[str] = None,
120
+ background_color: Optional[str] = None,
121
+ key: Optional[str] = None,
122
+ ) -> Element:
123
+ """Display an image from a resource path or URL."""
124
+ props = _filter_none(
125
+ source=source or None,
126
+ width=width,
127
+ height=height,
128
+ scale_type=scale_type,
129
+ background_color=background_color,
130
+ )
131
+ return Element("Image", props, [], key=key)
132
+
133
+
134
+ def Switch(
135
+ *,
136
+ value: bool = False,
137
+ on_change: Optional[Callable[[bool], None]] = None,
138
+ key: Optional[str] = None,
139
+ ) -> Element:
140
+ """Create a toggle switch."""
141
+ props: Dict[str, Any] = {"value": value}
142
+ if on_change is not None:
143
+ props["on_change"] = on_change
144
+ return Element("Switch", props, [], key=key)
145
+
146
+
147
+ def ProgressBar(
148
+ *,
149
+ value: float = 0.0,
150
+ background_color: Optional[str] = None,
151
+ key: Optional[str] = None,
152
+ ) -> Element:
153
+ """Show determinate progress (0.0 – 1.0)."""
154
+ props = _filter_none(value=value, background_color=background_color)
155
+ return Element("ProgressBar", props, [], key=key)
156
+
157
+
158
+ def ActivityIndicator(
159
+ *,
160
+ animating: bool = True,
161
+ key: Optional[str] = None,
162
+ ) -> Element:
163
+ """Show an indeterminate loading spinner."""
164
+ return Element("ActivityIndicator", {"animating": animating}, [], key=key)
165
+
166
+
167
+ def WebView(
168
+ *,
169
+ url: str = "",
170
+ key: Optional[str] = None,
171
+ ) -> Element:
172
+ """Embed web content."""
173
+ props: Dict[str, Any] = {}
174
+ if url:
175
+ props["url"] = url
176
+ return Element("WebView", props, [], key=key)
177
+
178
+
179
+ def Spacer(
180
+ *,
181
+ size: Optional[float] = None,
182
+ key: Optional[str] = None,
183
+ ) -> Element:
184
+ """Insert empty space with an optional fixed size."""
185
+ props = _filter_none(size=size)
186
+ return Element("Spacer", props, [], key=key)
187
+
188
+
189
+ # ---------------------------------------------------------------------------
190
+ # Container components
191
+ # ---------------------------------------------------------------------------
192
+
193
+ PaddingValue = Union[int, float, Dict[str, Union[int, float]]]
194
+
195
+
196
+ def Column(
197
+ *children: Element,
198
+ spacing: float = 0,
199
+ padding: Optional[PaddingValue] = None,
200
+ alignment: Optional[str] = None,
201
+ background_color: Optional[str] = None,
202
+ key: Optional[str] = None,
203
+ ) -> Element:
204
+ """Arrange children vertically."""
205
+ props = _filter_none(
206
+ spacing=spacing or None,
207
+ padding=padding,
208
+ alignment=alignment,
209
+ background_color=background_color,
210
+ )
211
+ return Element("Column", props, list(children), key=key)
212
+
213
+
214
+ def Row(
215
+ *children: Element,
216
+ spacing: float = 0,
217
+ padding: Optional[PaddingValue] = None,
218
+ alignment: Optional[str] = None,
219
+ background_color: Optional[str] = None,
220
+ key: Optional[str] = None,
221
+ ) -> Element:
222
+ """Arrange children horizontally."""
223
+ props = _filter_none(
224
+ spacing=spacing or None,
225
+ padding=padding,
226
+ alignment=alignment,
227
+ background_color=background_color,
228
+ )
229
+ return Element("Row", props, list(children), key=key)
230
+
231
+
232
+ def ScrollView(
233
+ child: Optional[Element] = None,
234
+ *,
235
+ background_color: Optional[str] = None,
236
+ key: Optional[str] = None,
237
+ ) -> Element:
238
+ """Wrap a single child in a scrollable container."""
239
+ children = [child] if child is not None else []
240
+ props = _filter_none(background_color=background_color)
241
+ return Element("ScrollView", props, children, key=key)
@@ -0,0 +1,47 @@
1
+ """Lightweight element descriptors for the virtual view tree.
2
+
3
+ An Element is an immutable description of a UI node — analogous to a React
4
+ element. It captures a type name, a props dictionary, and an ordered list
5
+ of children without creating any native platform objects. The reconciler
6
+ consumes these trees to determine what native views must be created,
7
+ updated, or removed.
8
+ """
9
+
10
+ from typing import Any, Dict, List, Optional
11
+
12
+
13
+ class Element:
14
+ """Immutable description of a single UI node."""
15
+
16
+ __slots__ = ("type", "props", "children", "key")
17
+
18
+ def __init__(
19
+ self,
20
+ type_name: str,
21
+ props: Dict[str, Any],
22
+ children: List["Element"],
23
+ key: Optional[str] = None,
24
+ ) -> None:
25
+ self.type = type_name
26
+ self.props = props
27
+ self.children = children
28
+ self.key = key
29
+
30
+ def __repr__(self) -> str:
31
+ return f"Element({self.type!r}, props={set(self.props)}, children={len(self.children)})"
32
+
33
+ def __eq__(self, other: object) -> bool:
34
+ if not isinstance(other, Element):
35
+ return NotImplemented
36
+ return (
37
+ self.type == other.type
38
+ and self.props == other.props
39
+ and self.children == other.children
40
+ and self.key == other.key
41
+ )
42
+
43
+ def __ne__(self, other: object) -> bool:
44
+ result = self.__eq__(other)
45
+ if result is NotImplemented:
46
+ return result
47
+ return not result