| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d9dd821 commit 4c06b67
18 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,11 +7,11 @@ def __init__(self, native_instance): | |||
| 7 | 7 | ||
| 8 | 8 | def on_create(self): | |
| 9 | 9 | super().on_create() | |
| 10 | - stack_view = pn.StackView(self.native_instance) | ||
| 10 | + stack_view = pn.StackView() | ||
| 11 | 11 | # list_data = ["item_{}".format(i) for i in range(100)] | |
| 12 | 12 | # list_view = pn.ListView(self.native_instance, list_data) | |
| 13 | 13 | # stack_view.add_view(list_view) | |
| 14 | - button = pn.Button(self.native_instance, "Button") | ||
| 14 | + button = pn.Button("Button") | ||
| 15 | 15 | button.set_on_click(lambda: self.navigate_to("")) | |
| 16 | 16 | # button.set_on_click(lambda: print("Button was clicked!")) | |
| 17 | 17 | stack_view.add_view(button) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,8 +7,8 @@ def __init__(self, native_instance): | |||
| 7 | 7 | ||
| 8 | 8 | def on_create(self): | |
| 9 | 9 | super().on_create() | |
| 10 | - stack_view = pn.StackView(self.native_instance) | ||
| 11 | - label = pn.Label(self.native_instance, "Second page!") | ||
| 10 | + stack_view = pn.StackView() | ||
| 11 | + label = pn.Label("Second page!") | ||
| 12 | 12 | stack_view.add_view(label) | |
| 13 | 13 | self.set_root_view(stack_view) | |
| 14 | 14 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,9 @@ | |||
| 1 | 1 | # pythonnative package | |
| 2 | 2 | ||
| 3 | 3 | API reference will be generated here via mkdocstrings. | |
| 4 | + | ||
| 5 | + Key flags and helpers (0.2.0): | ||
| 6 | + | ||
| 7 | + - `pythonnative.utils.IS_ANDROID`: platform flag with robust detection for Chaquopy/Android. | ||
| 8 | + - `pythonnative.utils.get_android_context()`: returns the current Android Activity/Context when running on Android. | ||
| 9 | + - `pythonnative.utils.set_android_context(ctx)`: set by `pythonnative.Page` on Android; you generally don’t call this directly. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,31 @@ | |||
| 1 | 1 | # Components | |
| 2 | 2 | ||
| 3 | 3 | High-level overview of PythonNative components and how they map to native UI. | |
| 4 | + | ||
| 5 | + ## Constructor pattern (0.2.0) | ||
| 6 | + | ||
| 7 | + - All core components share a consistent, contextless constructor on both platforms. | ||
| 8 | + - On Android, a `Context` is acquired implicitly from the current `Activity` set by `pn.Page`. | ||
| 9 | + - On iOS, UIKit classes are allocated/initialized directly. | ||
| 10 | + | ||
| 11 | + Examples: | ||
| 12 | + | ||
| 13 | + ```python | ||
| 14 | + import pythonnative as pn | ||
| 15 | + | ||
| 16 | + class MainPage(pn.Page): | ||
| 17 | + def __init__(self, native_instance): | ||
| 18 | + super().__init__(native_instance) | ||
| 19 | + | ||
| 20 | + def on_create(self): | ||
| 21 | + super().on_create() | ||
| 22 | + stack = pn.StackView() | ||
| 23 | + stack.add_view(pn.Label("Hello")) | ||
| 24 | + stack.add_view(pn.Button("Tap me")) | ||
| 25 | + stack.add_view(pn.TextField("initial")) | ||
| 26 | + self.set_root_view(stack) | ||
| 27 | + ``` | ||
| 28 | + | ||
| 29 | + Notes: | ||
| 30 | + - `pn.Page` stores the Android `Activity` so components like `pn.Button()` and `pn.Label()` can construct their native counterparts. | ||
| 31 | + - If you construct views before the `Page` is created on Android, a runtime error will be raised because no `Context` is available. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,5 +10,6 @@ pn --help | |||
| 10 | 10 | - Scaffolds `app/`, `pythonnative.json`, `requirements.txt`, `.gitignore` | |
| 11 | 11 | - Run: `pn run android` or `pn run ios` | |
| 12 | 12 | - Uses bundled templates; copies your `app/` into the platform project | |
| 13 | + - On Android, `pn.Page` provides the Activity context so components can be created without passing a context | ||
| 13 | 14 | - Clean: `pn clean` | |
| 14 | 15 | - Removes the `build/` directory safely | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | from abc import ABC, abstractmethod | |
| 2 | 2 | ||
| 3 | - from .utils import IS_ANDROID | ||
| 3 | + from .utils import IS_ANDROID, get_android_context | ||
| 4 | 4 | from .view import ViewBase | |
| 5 | 5 | ||
| 6 | 6 | # ======================================== | |
@@ -31,10 +31,11 @@ def stop_animating(self) -> None: | |||
| 31 | 31 | from java import jclass | |
| 32 | 32 | ||
| 33 | 33 | class ActivityIndicatorView(ActivityIndicatorViewBase, ViewBase): | |
| 34 | - def __init__(self, context) -> None: | ||
| 34 | + def __init__(self) -> None: | ||
| 35 | 35 | super().__init__() | |
| 36 | 36 | self.native_class = jclass("android.widget.ProgressBar") | |
| 37 | 37 | # self.native_instance = self.native_class(context, None, android.R.attr.progressBarStyleLarge) | |
| 38 | + context = get_android_context() | ||
| 38 | 39 | self.native_instance = self.native_class(context) | |
| 39 | 40 | self.native_instance.setIndeterminate(True) | |
| 40 | 41 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | from abc import ABC, abstractmethod | |
| 2 | 2 | from typing import Callable | |
| 3 | 3 | ||
| 4 | - from .utils import IS_ANDROID | ||
| 4 | + from .utils import IS_ANDROID, get_android_context | ||
| 5 | 5 | from .view import ViewBase | |
| 6 | 6 | ||
| 7 | 7 | # ======================================== | |
@@ -36,9 +36,10 @@ def set_on_click(self, callback: Callable[[], None]) -> None: | |||
| 36 | 36 | from java import dynamic_proxy, jclass | |
| 37 | 37 | ||
| 38 | 38 | class Button(ButtonBase, ViewBase): | |
| 39 | - def __init__(self, context, title: str = "") -> None: | ||
| 39 | + def __init__(self, title: str = "") -> None: | ||
| 40 | 40 | super().__init__() | |
| 41 | 41 | self.native_class = jclass("android.widget.Button") | |
| 42 | + context = get_android_context() | ||
| 42 | 43 | self.native_instance = self.native_class(context) | |
| 43 | 44 | self.set_title(title) | |
| 44 | 45 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,8 +53,8 @@ def __init__(self, native_instance): | |||
| 53 | 53 | ||
| 54 | 54 | def on_create(self): | |
| 55 | 55 | super().on_create() | |
| 56 | - stack = pn.StackView(self.native_instance) | ||
| 57 | - stack.add_view(pn.Label(self.native_instance, \"Hello from PythonNative!\")) | ||
| 56 | + stack = pn.StackView() | ||
| 57 | + stack.add_view(pn.Label(\"Hello from PythonNative!\")) | ||
| 58 | 58 | self.set_root_view(stack) | |
| 59 | 59 | """ | |
| 60 | 60 | ) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | from abc import ABC, abstractmethod | |
| 2 | 2 | ||
| 3 | - from .utils import IS_ANDROID | ||
| 3 | + from .utils import IS_ANDROID, get_android_context | ||
| 4 | 4 | from .view import ViewBase | |
| 5 | 5 | ||
| 6 | 6 | # ======================================== | |
@@ -32,9 +32,10 @@ def get_image(self) -> str: | |||
| 32 | 32 | from java import jclass | |
| 33 | 33 | ||
| 34 | 34 | class ImageView(ImageViewBase, ViewBase): | |
| 35 | - def __init__(self, context, image: str = "") -> None: | ||
| 35 | + def __init__(self, image: str = "") -> None: | ||
| 36 | 36 | super().__init__() | |
| 37 | 37 | self.native_class = jclass("android.widget.ImageView") | |
| 38 | + context = get_android_context() | ||
| 38 | 39 | self.native_instance = self.native_class(context) | |
| 39 | 40 | if image: | |
| 40 | 41 | self.set_image(image) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | from abc import ABC, abstractmethod | |
| 2 | 2 | ||
| 3 | - from .utils import IS_ANDROID | ||
| 3 | + from .utils import IS_ANDROID, get_android_context | ||
| 4 | 4 | from .view import ViewBase | |
| 5 | 5 | ||
| 6 | 6 | # ======================================== | |
@@ -31,9 +31,10 @@ def get_text(self) -> str: | |||
| 31 | 31 | from java import jclass | |
| 32 | 32 | ||
| 33 | 33 | class Label(LabelBase, ViewBase): | |
| 34 | - def __init__(self, context, text: str = "") -> None: | ||
| 34 | + def __init__(self, text: str = "") -> None: | ||
| 35 | 35 | super().__init__() | |
| 36 | 36 | self.native_class = jclass("android.widget.TextView") | |
| 37 | + context = get_android_context() | ||
| 37 | 38 | self.native_instance = self.native_class(context) | |
| 38 | 39 | self.set_text(text) | |
| 39 | 40 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments