pyjs-examples 0.4.0__tar.gz → 0.5.0__tar.gz

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 (121) hide show
  1. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/PKG-INFO +13 -8
  2. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/README.md +9 -4
  3. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/calculator/README.md +20 -6
  4. pyjs_examples-0.5.0/calculator/__main__.py +24 -0
  5. pyjs_examples-0.5.0/calculator/android.py +163 -0
  6. pyjs_examples-0.5.0/calculator/android_build.py +27 -0
  7. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/calculator/calculator.py +28 -97
  8. pyjs_examples-0.5.0/calculator/model.py +124 -0
  9. pyjs_examples-0.5.0/calculator/tests/__init__.py +0 -0
  10. pyjs_examples-0.5.0/calculator/tests/test_android_calculator.py +53 -0
  11. pyjs_examples-0.5.0/calculator/tests/test_android_calculator_model.py +26 -0
  12. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/calculator/tests/test_browser_calculator.py +24 -3
  13. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/calculator/tests/test_calculator.py +2 -1
  14. pyjs_examples-0.5.0/calculator/tests/test_model.py +72 -0
  15. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/calculator/tests/test_pwa.py +24 -7
  16. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/chat/README.md +3 -0
  17. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/chat/chat.py +3 -3
  18. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/chat/tests/test_browser_chat.py +18 -2
  19. pyjs_examples-0.5.0/components/README.md +169 -0
  20. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/components/__main__.py +7 -1
  21. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/components/app.py +26 -3
  22. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/components/catalog.py +173 -9
  23. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/components/catalog_sources.py +20 -3
  24. pyjs_examples-0.5.0/components/diagram_viewer.py +69 -0
  25. pyjs_examples-0.5.0/components/diagrams.py +102 -0
  26. pyjs_examples-0.5.0/components/images.py +49 -0
  27. pyjs_examples-0.5.0/components/tests/test_browser_diagram_viewer.py +139 -0
  28. pyjs_examples-0.5.0/components/tests/test_browser_diagrams.py +116 -0
  29. pyjs_examples-0.5.0/components/tests/test_browser_navigation.py +435 -0
  30. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/components/tests/test_browser_workspace.py +32 -3
  31. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/components/tests/test_components.py +23 -1
  32. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/components/workspace.py +25 -1
  33. pyjs_examples-0.5.0/homescreen_widget/README.md +20 -0
  34. pyjs_examples-0.5.0/homescreen_widget/__init__.py +1 -0
  35. pyjs_examples-0.5.0/homescreen_widget/android.py +142 -0
  36. pyjs_examples-0.5.0/homescreen_widget/android_build.py +42 -0
  37. pyjs_examples-0.5.0/homescreen_widget/res/layout/note_widget_item.xml +13 -0
  38. pyjs_examples-0.5.0/homescreen_widget/res/layout/notes_widget.xml +36 -0
  39. pyjs_examples-0.5.0/homescreen_widget/res/values/strings.xml +6 -0
  40. pyjs_examples-0.5.0/homescreen_widget/res/xml/notes_widget_info.xml +11 -0
  41. pyjs_examples-0.5.0/notes/README.md +152 -0
  42. pyjs_examples-0.5.0/notes/__init__.py +1 -0
  43. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/notes/__main__.py +7 -4
  44. pyjs_examples-0.5.0/notes/android.py +401 -0
  45. pyjs_examples-0.5.0/notes/android_build.py +39 -0
  46. pyjs_examples-0.5.0/notes/android_datetime.py +81 -0
  47. pyjs_examples-0.5.0/notes/android_saves.py +52 -0
  48. pyjs_examples-0.5.0/notes/android_storage.py +222 -0
  49. pyjs_examples-0.5.0/notes/share.xml +12 -0
  50. pyjs_examples-0.5.0/notes/tests/test_android_model.py +20 -0
  51. pyjs_examples-0.5.0/notes/tests/test_android_save_queue.py +232 -0
  52. pyjs_examples-0.5.0/notes/tests/test_android_saves.py +113 -0
  53. pyjs_examples-0.5.0/notes/tests/test_android_storage.py +335 -0
  54. pyjs_examples-0.5.0/notes/tests/test_android_ui.py +700 -0
  55. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/pyjs_examples.egg-info/PKG-INFO +13 -8
  56. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/pyjs_examples.egg-info/SOURCES.txt +56 -20
  57. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/pyjs_examples.egg-info/requires.txt +3 -3
  58. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/pyproject.toml +7 -6
  59. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/space_invaders/space_invaders.py +2 -2
  60. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/README.md +13 -3
  61. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/api.py +66 -9
  62. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/core.py +8 -0
  63. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/server.py +2 -2
  64. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/store.py +119 -16
  65. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/tests/test_browser_store.py +31 -0
  66. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/tests/test_store.py +66 -5
  67. pyjs_examples-0.4.0/calculator/__main__.py +0 -21
  68. pyjs_examples-0.4.0/calculator/android.py +0 -225
  69. pyjs_examples-0.4.0/calculator/tests/test_android_calculator.py +0 -34
  70. pyjs_examples-0.4.0/calculator/tests/test_android_calculator_model.py +0 -26
  71. pyjs_examples-0.4.0/components/README.md +0 -38
  72. pyjs_examples-0.4.0/components/tests/test_browser_navigation.py +0 -58
  73. pyjs_examples-0.4.0/notes/README.md +0 -48
  74. pyjs_examples-0.4.0/notes/__init__.py +0 -5
  75. pyjs_examples-0.4.0/notes/mobile_assets/fonts/LICENSE +0 -202
  76. pyjs_examples-0.4.0/notes/mobile_assets/fonts/MaterialIcons-Regular.ttf +0 -0
  77. pyjs_examples-0.4.0/notes/notes.py +0 -1016
  78. pyjs_examples-0.4.0/notes/tests/renders/android/notes-actions.png +0 -0
  79. pyjs_examples-0.4.0/notes/tests/renders/android/notes-checklist-editor.png +0 -0
  80. pyjs_examples-0.4.0/notes/tests/renders/android/notes-home.png +0 -0
  81. pyjs_examples-0.4.0/notes/tests/renders/android/notes-list.png +0 -0
  82. pyjs_examples-0.4.0/notes/tests/renders/android/notes-navigation.png +0 -0
  83. pyjs_examples-0.4.0/notes/tests/test_android_notes.py +0 -116
  84. pyjs_examples-0.4.0/notes/tests/test_notes.py +0 -181
  85. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/LICENSE +0 -0
  86. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/__init__.py +0 -0
  87. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/calculator/__init__.py +0 -0
  88. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/chat/__init__.py +0 -0
  89. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/chat/__main__.py +0 -0
  90. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/chat/chat_server.py +0 -0
  91. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/chat/protocol.py +0 -0
  92. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/chat/tests/test_chat_server.py +0 -0
  93. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/chat/visual_test.py +0 -0
  94. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/components/__init__.py +0 -0
  95. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/components/tests/test_workspace.py +0 -0
  96. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/counter.py +0 -0
  97. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/notes/tests/renders/notes-board.png +0 -0
  98. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/notes/tests/test_browser_web_notes.py +0 -0
  99. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/notes/tests/test_web_notes.py +0 -0
  100. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/notes/visual_test.py +0 -0
  101. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/notes/web.py +0 -0
  102. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/pyjs_examples.egg-info/dependency_links.txt +0 -0
  103. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/pyjs_examples.egg-info/entry_points.txt +0 -0
  104. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/pyjs_examples.egg-info/top_level.txt +0 -0
  105. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/setup.cfg +0 -0
  106. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/space_invaders/README.md +0 -0
  107. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/space_invaders/__init__.py +0 -0
  108. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/space_invaders/__main__.py +0 -0
  109. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/space_invaders/tests/renders/action_scene.png +0 -0
  110. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/space_invaders/tests/renders/initial_wave.png +0 -0
  111. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/space_invaders/tests/test_browser_game.py +0 -0
  112. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/space_invaders/tests/test_game.py +0 -0
  113. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/__init__.py +0 -0
  114. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/__main__.py +0 -0
  115. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/apps.py +0 -0
  116. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/fixtures/store.json +0 -0
  117. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/models.py +0 -0
  118. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/settings.py +0 -0
  119. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/tests/conftest.py +0 -0
  120. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/urls.py +0 -0
  121. {pyjs_examples-0.4.0 → pyjs_examples-0.5.0}/store/visual_test.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyjs-examples
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Summary: Official example applications for PyJS.
5
5
  Author-email: Lex Berezhny <lex@damoti.com>
6
6
  License-Expression: BSD-3-Clause
@@ -19,16 +19,16 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
19
  Requires-Python: >=3.14
20
20
  Description-Content-Type: text/markdown
21
21
  License-File: LICENSE
22
- Requires-Dist: pyjs==0.4.0
22
+ Requires-Dist: pyjs==0.5.0
23
23
  Provides-Extra: store
24
24
  Requires-Dist: django<6.2,>=6.0; extra == "store"
25
25
  Requires-Dist: pydantic<3,>=2; extra == "store"
26
- Requires-Dist: pyjs-extras==0.4.0; extra == "store"
26
+ Requires-Dist: pyjs-extras==0.5.0; extra == "store"
27
27
  Requires-Dist: uvicorn<1,>=0.52; extra == "store"
28
28
  Provides-Extra: test
29
29
  Requires-Dist: django<6.2,>=6.0; extra == "test"
30
30
  Requires-Dist: pydantic<3,>=2; extra == "test"
31
- Requires-Dist: pyjs-extras==0.4.0; extra == "test"
31
+ Requires-Dist: pyjs-extras==0.5.0; extra == "test"
32
32
  Requires-Dist: pytest>=9.0; extra == "test"
33
33
  Requires-Dist: uvicorn<1,>=0.52; extra == "test"
34
34
  Dynamic: license-file
@@ -62,8 +62,9 @@ Features may overlap when that makes an application more realistic.
62
62
  | [Calculator](calculator/) | Universal app | Web, PWA, Android, SSR, state |
63
63
  | [Components](components/) | UI reference | Widgets, forms, navigation |
64
64
  | [Notes](notes/) | Personal app | Web, Android, drag/drop, restore |
65
+ | [Home screen widget](homescreen_widget/) | Android sample | RemoteViews |
65
66
  | [Chat](chat/) | Real-time starter | WebSockets, multi-window, plans |
66
- | [Store](store/) | Business app | Django, JSON:API, SSE, themes, cursor feeds |
67
+ | [Store](store/) | Business app | Django, JSON:API, streams, cursor feeds |
67
68
  | [Space Invaders](space_invaders/) | GPU game | WebGPU, input, render tests |
68
69
 
69
70
  Run an application with its module name, for example:
@@ -78,8 +79,11 @@ python -m pyjs_examples.store
78
79
 
79
80
  Install `pyjs-examples[store]` before running Store from a release package.
80
81
 
81
- Calculator and Notes accept `--android` to build and launch their NativeScript
82
- versions. Their default command serves the web version.
82
+ Calculator and Notes accept `--android` to build and launch their direct-JVM
83
+ Android versions. Their default command serves the web version.
84
+
85
+ Home screen widget is a direct-JVM example rather than a template application.
86
+ Its local README shows the small build command for its signed APK.
83
87
 
84
88
  Components is also the source and test surface for the planned website
85
89
  component reference. It combines the former catalog, workspace, navigation,
@@ -90,7 +94,8 @@ uses DOM-owned ordering, drag/drop, server restoration, and visual tests.
90
94
 
91
95
  Store is the canonical Django and typed client/server example. It also owns the
92
96
  themability story, signed cursor feeds, infinite scrolling, and data-driven
93
- charts.
97
+ charts. Its visitor page demonstrates snapshot-backed typed streams and a
98
+ bounded plain-text log viewer.
94
99
 
95
100
  ## Templates
96
101
 
@@ -27,8 +27,9 @@ Features may overlap when that makes an application more realistic.
27
27
  | [Calculator](calculator/) | Universal app | Web, PWA, Android, SSR, state |
28
28
  | [Components](components/) | UI reference | Widgets, forms, navigation |
29
29
  | [Notes](notes/) | Personal app | Web, Android, drag/drop, restore |
30
+ | [Home screen widget](homescreen_widget/) | Android sample | RemoteViews |
30
31
  | [Chat](chat/) | Real-time starter | WebSockets, multi-window, plans |
31
- | [Store](store/) | Business app | Django, JSON:API, SSE, themes, cursor feeds |
32
+ | [Store](store/) | Business app | Django, JSON:API, streams, cursor feeds |
32
33
  | [Space Invaders](space_invaders/) | GPU game | WebGPU, input, render tests |
33
34
 
34
35
  Run an application with its module name, for example:
@@ -43,8 +44,11 @@ python -m pyjs_examples.store
43
44
 
44
45
  Install `pyjs-examples[store]` before running Store from a release package.
45
46
 
46
- Calculator and Notes accept `--android` to build and launch their NativeScript
47
- versions. Their default command serves the web version.
47
+ Calculator and Notes accept `--android` to build and launch their direct-JVM
48
+ Android versions. Their default command serves the web version.
49
+
50
+ Home screen widget is a direct-JVM example rather than a template application.
51
+ Its local README shows the small build command for its signed APK.
48
52
 
49
53
  Components is also the source and test surface for the planned website
50
54
  component reference. It combines the former catalog, workspace, navigation,
@@ -55,7 +59,8 @@ uses DOM-owned ordering, drag/drop, server restoration, and visual tests.
55
59
 
56
60
  Store is the canonical Django and typed client/server example. It also owns the
57
61
  themability story, signed cursor feeds, infinite scrolling, and data-driven
58
- charts.
62
+ charts. Its visitor page demonstrates snapshot-backed typed streams and a
63
+ bounded plain-text log viewer.
59
64
 
60
65
  ## Templates
61
66
 
@@ -5,7 +5,7 @@ example for pyjs browser-target testing.
5
5
 
6
6
  It demonstrates:
7
7
 
8
- - calculator state and behavior owned by a custom element
8
+ - a shared calculator model with browser and native Android views
9
9
  - typed `state()` fields serialized to DOM attributes and rendered with
10
10
  `@effect`
11
11
  - a centered responsive custom-element UI
@@ -15,7 +15,7 @@ It demonstrates:
15
15
  - asynchronous result persistence through an HTTP POST route
16
16
  - per-browser in-memory sessions that restore the last result after refresh
17
17
  - a Python-authored service worker with cache-first offline application loading
18
- - a native Android implementation using the same arithmetic primitive
18
+ - a native Android implementation using the same state machine
19
19
  - browser tests that interact with the real DOM through button events
20
20
 
21
21
  The example intentionally stays focused on calculator behavior. Reusable widget
@@ -37,13 +37,24 @@ the server clears them. The app also registers `/calculator-serviceworker.js`.
37
37
  After its first controlled reload, the calculator can load and calculate while
38
38
  the browser is offline.
39
39
 
40
- Run the native Android version on an existing emulator, or let the managed
41
- NativeScript tooling start its configured device:
40
+ Run the native Android version with an Android SDK and JDK installed. PyJS
41
+ builds a signed debug APK and uses a running or configured emulator:
42
42
 
43
43
  ```console
44
44
  python -m pyjs_examples.calculator --android
45
45
  ```
46
46
 
47
+ `model.py` owns number entry, arithmetic, operator selection, and error
48
+ recovery. Both views dispatch key actions to its `Calculator` class. The web
49
+ view copies its DOM-backed state into the model for each action and writes the
50
+ result back, preserving server rendering and hydration. Android keeps a model
51
+ instance and updates its native display after each action. The two numeric
52
+ conversion helpers use Python/JavaScript conversion with explicit JVM bindings.
53
+
54
+ `android.py` implements the native controls and `android_build.py`
55
+ packages them through D8 and AAPT2. This is the only Android implementation;
56
+ there is no JavaScript runtime or Gradle project in the app.
57
+
47
58
  ## Run the tests
48
59
 
49
60
  From the repository root, run the Python target:
@@ -52,7 +63,7 @@ From the repository root, run the Python target:
52
63
  pytest -q examples/calculator/tests
53
64
  ```
54
65
 
55
- Run the same model tests plus the browser-only UI tests in the browser:
66
+ Run the browser UI tests and shared model scenarios in the browser:
56
67
 
57
68
  ```bash
58
69
  pytest -q examples/calculator/tests --pyjs-target=web
@@ -70,5 +81,8 @@ Run the Android interaction test with:
70
81
 
71
82
  ```console
72
83
  pytest -q examples/calculator/tests/test_android_calculator.py \
73
- --pyjs-target=android --pyjs-android-avd=Routine_Test
84
+ --pyjs-target=android --pyjs-android-runtime=robolectric
74
85
  ```
86
+
87
+ Use `--pyjs-android-runtime=device --pyjs-android-avd=YOUR_AVD` instead
88
+ for the emulator tier.
@@ -0,0 +1,24 @@
1
+ import argparse
2
+ import os
3
+
4
+ parser = argparse.ArgumentParser(description="Run the Calculator example.")
5
+ parser.add_argument("--android", action="store_true", help="Build and run the Android APK")
6
+
7
+ if parser.parse_args().android:
8
+ from .android_build import run_android
9
+
10
+ run_android()
11
+ else:
12
+ from pyjs.server.dev import serve
13
+
14
+ from .calculator import render, save_result, service_worker_javascript
15
+
16
+ serve(
17
+ "pyjs_examples.calculator.calculator",
18
+ render=render,
19
+ routes={
20
+ ("POST", "/api/calculator/result"): save_result,
21
+ ("GET", "/calculator-serviceworker.js"): service_worker_javascript,
22
+ },
23
+ port=int(os.environ.get("PYJS_PORT", "8000")),
24
+ )
@@ -0,0 +1,163 @@
1
+ """Native Android Calculator implementation."""
2
+
3
+ from pyjs.android import (
4
+ Activity,
5
+ Bundle,
6
+ Button,
7
+ Context,
8
+ Gravity,
9
+ LayoutParams,
10
+ LinearLayout,
11
+ LinearLayoutParams,
12
+ OnClickListener,
13
+ TextView,
14
+ View,
15
+ parse_color,
16
+ )
17
+ from pyjs.android.controls import ScrollView
18
+ from pyjs.java import f32, i32
19
+
20
+ from .model import Calculator
21
+
22
+
23
+ class CalculatorClickListener(OnClickListener):
24
+ def __init__(
25
+ self,
26
+ calculator: AndroidCalculator,
27
+ kind: str,
28
+ value: str,
29
+ ) -> None:
30
+ self.calculator = calculator
31
+ self.kind = kind
32
+ self.value = value
33
+
34
+ def onClick(self, view: View) -> None:
35
+ self.calculator.press(self.kind, self.value)
36
+
37
+
38
+ class AndroidCalculator:
39
+ def __init__(self, context: Context) -> None:
40
+ self.context = context
41
+ self.calculator = Calculator()
42
+
43
+ self.display = TextView(context)
44
+ self.display.setText(self.calculator.display_value)
45
+ self.display.setTextSize(f32(40.0))
46
+ self.display.setGravity(Gravity.END)
47
+ self.display.setPadding(i32(24), i32(32), i32(24), i32(32))
48
+ self.display.setBackgroundColor(parse_color("#020617"))
49
+
50
+ def press(self, kind: str, value: str) -> None:
51
+ self.calculator.press(kind, value)
52
+ self.display.setText(self.calculator.display_value)
53
+
54
+ def key(self, label: str, value: str, kind: str) -> Button:
55
+ key = Button(self.context)
56
+ key.setText(label)
57
+ key.setTextSize(f32(22.0))
58
+ key.setContentDescription(label)
59
+ key.setPadding(i32(12), i32(16), i32(12), i32(16))
60
+ key.setOnClickListener(CalculatorClickListener(self, kind, value))
61
+ return key
62
+
63
+ def page(self) -> ScrollView:
64
+ root = LinearLayout(self.context)
65
+ root.setOrientation(LinearLayout.VERTICAL)
66
+ root.setGravity(Gravity.CENTER)
67
+ root.setPadding(i32(20), i32(20), i32(20), i32(20))
68
+ root.setBackgroundColor(parse_color("#0f172a"))
69
+ root.setLayoutParams(
70
+ LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
71
+ )
72
+
73
+ title = TextView(self.context)
74
+ title.setText("PyJS Calculator")
75
+ title.setTextSize(f32(24.0))
76
+ title.setGravity(Gravity.CENTER_HORIZONTAL)
77
+ root.addView(title)
78
+ root.addView(self.display)
79
+
80
+ keypad = LinearLayout(self.context)
81
+ keypad.setOrientation(LinearLayout.VERTICAL)
82
+ keypad.setLayoutParams(
83
+ LinearLayoutParams(
84
+ LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, f32(0.0)
85
+ )
86
+ )
87
+ labels = [
88
+ ("AC", "clear", "action"),
89
+ ("±", "sign", "action"),
90
+ ("%", "percent", "action"),
91
+ ("÷", "/", "operator"),
92
+ ("7", "7", "digit"),
93
+ ("8", "8", "digit"),
94
+ ("9", "9", "digit"),
95
+ ("×", "*", "operator"),
96
+ ("4", "4", "digit"),
97
+ ("5", "5", "digit"),
98
+ ("6", "6", "digit"),
99
+ ("−", "-", "operator"),
100
+ ("1", "1", "digit"),
101
+ ("2", "2", "digit"),
102
+ ("3", "3", "digit"),
103
+ ("+", "+", "operator"),
104
+ ("0", "0", "digit"),
105
+ (".", ".", "decimal"),
106
+ ("=", "=", "equals"),
107
+ ]
108
+ row = LinearLayout(self.context)
109
+ index = 0
110
+ for label, value, kind in labels:
111
+ if index % 4 == 0:
112
+ row = LinearLayout(self.context)
113
+ row.setOrientation(LinearLayout.HORIZONTAL)
114
+ row.setLayoutParams(
115
+ LinearLayoutParams(
116
+ LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, f32(0.0)
117
+ )
118
+ )
119
+ keypad.addView(row)
120
+ key = self.key(label, value, kind)
121
+ key.setId(i32(100 + index))
122
+ key.setLayoutParams(
123
+ LinearLayoutParams(i32(0), LayoutParams.WRAP_CONTENT, f32(1.0))
124
+ )
125
+ row.addView(key)
126
+ index += 1
127
+ root.addView(keypad)
128
+ scroll = ScrollView(self.context)
129
+ scroll.setFitsSystemWindows(True)
130
+ scroll.set_fill_viewport(True)
131
+ scroll.addView(root)
132
+ return scroll
133
+
134
+
135
+ class CalculatorActivity(Activity):
136
+ def onCreate(self, state: Bundle | None) -> None:
137
+ super().onCreate(state)
138
+ self.setTitle("PyJS Calculator")
139
+ self.view = AndroidCalculator(self)
140
+ calculator = self.view.calculator
141
+ if state is not None:
142
+ calculator.display_value = state.get_string("display") or "0"
143
+ calculator.stored_value = state.get_double("stored", 0.0)
144
+ calculator.pending_operator = state.get_string("operator") or ""
145
+ calculator.replace_display = state.get_boolean("replace", True)
146
+ calculator.error = state.get_boolean("error", False)
147
+ self.view.display.setText(calculator.display_value)
148
+ self.setContentView(self.view.page())
149
+
150
+ def onSaveInstanceState(self, state: Bundle) -> None:
151
+ calculator = self.view.calculator
152
+ state.put_string("display", calculator.display_value)
153
+ state.put_double("stored", calculator.stored_value)
154
+ state.put_string("operator", calculator.pending_operator)
155
+ state.put_boolean("replace", calculator.replace_display)
156
+ state.put_boolean("error", calculator.error)
157
+ super().onSaveInstanceState(state)
158
+
159
+
160
+ def main() -> int:
161
+ activity = CalculatorActivity()
162
+ activity.onCreate(Bundle())
163
+ return 0
@@ -0,0 +1,27 @@
1
+ from pathlib import Path
2
+
3
+ from pyjs.tooling.android import AndroidSDK
4
+ from pyjs.tooling.android_jvm import AndroidJVMApplication, AndroidJVMBuilder
5
+
6
+
7
+ def application() -> AndroidJVMApplication:
8
+ return AndroidJVMApplication(
9
+ Path(__file__).with_name("android.py"),
10
+ "pyjs_examples.calculator.android",
11
+ "org.pyjs.calculator",
12
+ "CalculatorActivity",
13
+ "PyJS Calculator",
14
+ compile_sdk=35,
15
+ )
16
+
17
+
18
+ def run_android(output: Path | None = None) -> Path:
19
+ sdk = AndroidSDK()
20
+ app = application()
21
+ package = AndroidJVMBuilder(sdk).build(
22
+ app, output or Path("build/android-calculator"),
23
+ )
24
+ device = sdk.ensure_emulator()
25
+ sdk.install(device, package)
26
+ sdk.launch(device, app.app_id, app.android_activity)
27
+ return package
@@ -11,16 +11,7 @@ from pyjs.web.serviceworker import (
11
11
  )
12
12
  from pyjs.web.ui import Widget
13
13
 
14
-
15
- @js
16
- def calculate_result(left: float, right: float, operator: str) -> float:
17
- if operator == "+":
18
- return left + right
19
- if operator == "-":
20
- return left - right
21
- if operator == "*":
22
- return left * right
23
- return left / right
14
+ from .model import Calculator
24
15
 
25
16
 
26
17
  @js
@@ -83,8 +74,8 @@ def service_worker_main():
83
74
 
84
75
  @nojs
85
76
  def service_worker_javascript(request):
77
+ from pyjs.javascript.transpiler import bundle
86
78
  from pyjs.server.dev import Response
87
- from pyjs.transpiler import bundle
88
79
 
89
80
  source, _ = bundle(service_worker_main, include_main=True, include_css=False)
90
81
  return Response(
@@ -281,83 +272,46 @@ class CalculatorApp(Widget):
281
272
  async def initialize(self):
282
273
  if self.service_worker_url == "":
283
274
  return
284
- registration = await navigator.serviceWorker.register(self.service_worker_url)
275
+ await navigator.serviceWorker.register(self.service_worker_url)
276
+ registration = await navigator.serviceWorker.ready
285
277
  self.set_data("service-worker", registration.scope)
286
278
 
287
279
  @effect(display_value)
288
280
  def render_display(self):
289
281
  self.display.textContent = self.display_value
290
282
 
283
+ def read_model(self) -> Calculator:
284
+ calculator = Calculator(self.display_value)
285
+ calculator.stored_value = self.stored_value
286
+ calculator.pending_operator = self.pending_operator
287
+ calculator.replace_display = self.replace_display
288
+ calculator.error = self.error
289
+ return calculator
290
+
291
291
  def clear(self):
292
- self.display_value = "0"
293
- self.stored_value = 0.0
294
- self.pending_operator = ""
295
- self.replace_display = True
296
- self.error = False
292
+ self.press("action", "clear")
297
293
 
298
294
  def value(self) -> float:
299
- if self.error:
300
- return 0.0
301
- return float(self.display_value)
295
+ return self.read_model().value()
302
296
 
303
297
  def input_digit(self, digit: str):
304
- if self.error:
305
- self.clear()
306
- if self.replace_display or self.display_value == "0":
307
- self.display_value = digit
308
- self.replace_display = False
309
- else:
310
- self.display_value += digit
298
+ self.press("digit", digit)
311
299
 
312
300
  def input_decimal(self):
313
- if self.error:
314
- self.clear()
315
- if self.replace_display:
316
- self.display_value = "0."
317
- self.replace_display = False
318
- elif len(self.display_value.split(".")) == 1:
319
- self.display_value += "."
301
+ self.press("decimal", ".")
320
302
 
321
303
  def toggle_sign(self):
322
- value = self.display_value
323
- if self.error or value == "0" or value == "0.0":
324
- return
325
- self.display_value = str(0.0 - float(value))
304
+ self.press("action", "sign")
326
305
 
327
306
  def percent(self):
328
- if self.error:
329
- return
330
- self.display_value = str(float(self.display_value) / 100.0)
331
- self.replace_display = True
307
+ self.press("action", "percent")
332
308
 
333
309
  def choose_operator(self, operator: str):
334
- if self.error:
335
- return
336
- if self.pending_operator != "" and not self.replace_display:
337
- self.calculate()
338
- if self.error:
339
- return
340
- self.stored_value = float(self.display_value)
341
- self.pending_operator = operator
342
- self.replace_display = True
310
+ self.press("operator", operator)
343
311
 
344
312
  def calculate(self) -> float:
345
- operator = self.pending_operator
346
- if self.error or operator == "":
347
- return self.value()
348
- right = float(self.display_value)
349
- if operator == "/" and right == 0.0:
350
- self.display_value = "Error"
351
- self.pending_operator = ""
352
- self.replace_display = True
353
- self.error = True
354
- return 0.0
355
- result = calculate_result(self.stored_value, right, operator)
356
- self.display_value = str(result)
357
- self.stored_value = result
358
- self.pending_operator = ""
359
- self.replace_display = True
360
- return result
313
+ self.press("equals", "=")
314
+ return self.value()
361
315
 
362
316
  async def persist_result(self):
363
317
  if self.save_url == "":
@@ -373,20 +327,13 @@ class CalculatorApp(Widget):
373
327
  )
374
328
 
375
329
  def press(self, kind: str, value: str):
376
- if kind == "digit":
377
- self.input_digit(value)
378
- elif kind == "decimal":
379
- self.input_decimal()
380
- elif kind == "operator":
381
- self.choose_operator(value)
382
- elif kind == "equals":
383
- self.calculate()
384
- elif value == "clear":
385
- self.clear()
386
- elif value == "sign":
387
- self.toggle_sign()
388
- else:
389
- self.percent()
330
+ calculator = self.read_model()
331
+ calculator.press(kind, value)
332
+ self.stored_value = calculator.stored_value
333
+ self.pending_operator = calculator.pending_operator
334
+ self.replace_display = calculator.replace_display
335
+ self.error = calculator.error
336
+ self.display_value = calculator.display_value
390
337
 
391
338
 
392
339
  def main(
@@ -420,19 +367,3 @@ def save_result(request):
420
367
  if not math.isfinite(float(result)):
421
368
  raise ValueError("result must be finite")
422
369
  request.session["last_result"] = result
423
-
424
-
425
- if __name__ == "__main__":
426
- import os
427
-
428
- from pyjs.server.dev import serve
429
-
430
- serve(
431
- "calculator",
432
- render=render,
433
- routes={
434
- ("POST", "/api/calculator/result"): save_result,
435
- ("GET", "/calculator-serviceworker.js"): service_worker_javascript,
436
- },
437
- port=int(os.environ.get("PYJS_PORT", "8000")),
438
- )
@@ -0,0 +1,124 @@
1
+ """Calculator behavior shared by the browser and native Android views."""
2
+
3
+ from pyjs import js, jvm
4
+
5
+
6
+ @js
7
+ @jvm.native("java/lang/Double", name="parseDouble")
8
+ def parse_number(value: str) -> float:
9
+ return float(value)
10
+
11
+
12
+ @js
13
+ @jvm.native("java/lang/Double", name="toString")
14
+ def format_number(value: float) -> str:
15
+ return str(value)
16
+
17
+
18
+ @js
19
+ def calculate_result(left: float, right: float, operator: str) -> float:
20
+ if operator == "+":
21
+ return left + right
22
+ if operator == "-":
23
+ return left - right
24
+ if operator == "*":
25
+ return left * right
26
+ return left / right
27
+
28
+
29
+ @js
30
+ class Calculator:
31
+ def __init__(self, initial_value: str = "0") -> None:
32
+ self.display_value = initial_value
33
+ self.stored_value = 0.0
34
+ self.pending_operator = ""
35
+ self.replace_display = True
36
+ self.error = False
37
+
38
+ def clear(self):
39
+ self.display_value = "0"
40
+ self.stored_value = 0.0
41
+ self.pending_operator = ""
42
+ self.replace_display = True
43
+ self.error = False
44
+
45
+ def value(self) -> float:
46
+ if self.error:
47
+ return 0.0
48
+ return parse_number(self.display_value)
49
+
50
+ def input_digit(self, digit: str):
51
+ if self.error:
52
+ self.clear()
53
+ if self.replace_display or self.display_value == "0":
54
+ self.display_value = digit
55
+ self.replace_display = False
56
+ else:
57
+ self.display_value = self.display_value + digit
58
+
59
+ def input_decimal(self):
60
+ if self.error:
61
+ self.clear()
62
+ value = self.display_value
63
+ if self.replace_display:
64
+ self.display_value = "0."
65
+ self.replace_display = False
66
+ elif "." not in value:
67
+ self.display_value = value + "."
68
+
69
+ def toggle_sign(self):
70
+ value = self.display_value
71
+ if self.error or value == "0" or value == "0.0":
72
+ return
73
+ self.display_value = format_number(0.0 - parse_number(value))
74
+
75
+ def percent(self):
76
+ if self.error:
77
+ return
78
+ self.display_value = format_number(parse_number(self.display_value) / 100.0)
79
+ self.replace_display = True
80
+
81
+ def choose_operator(self, operator: str):
82
+ if self.error:
83
+ return
84
+ if self.pending_operator != "" and not self.replace_display:
85
+ self.calculate()
86
+ if self.error:
87
+ return
88
+ self.stored_value = parse_number(self.display_value)
89
+ self.pending_operator = operator
90
+ self.replace_display = True
91
+
92
+ def calculate(self) -> float:
93
+ operator = self.pending_operator
94
+ if self.error or operator == "":
95
+ return self.value()
96
+ right = parse_number(self.display_value)
97
+ if operator == "/" and right == 0.0:
98
+ self.display_value = "Error"
99
+ self.pending_operator = ""
100
+ self.replace_display = True
101
+ self.error = True
102
+ return 0.0
103
+ result = calculate_result(self.stored_value, right, operator)
104
+ self.display_value = format_number(result)
105
+ self.stored_value = result
106
+ self.pending_operator = ""
107
+ self.replace_display = True
108
+ return result
109
+
110
+ def press(self, kind: str, value: str):
111
+ if kind == "digit":
112
+ self.input_digit(value)
113
+ elif kind == "decimal":
114
+ self.input_decimal()
115
+ elif kind == "operator":
116
+ self.choose_operator(value)
117
+ elif kind == "equals":
118
+ self.calculate()
119
+ elif value == "clear":
120
+ self.clear()
121
+ elif value == "sign":
122
+ self.toggle_sign()
123
+ else:
124
+ self.percent()
File without changes