1
0
mirror of https://github.com/pyscript/pyscript.git synced 2022-05-01 19:47:48 +03:00

Merge pull request #29 from anaconda/toga

Add a Toga web demo
This commit is contained in:
Fabio Pliger
2022-04-19 11:24:19 -05:00
committed by GitHub
20 changed files with 27483 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
# Freedom Units!
This is a demo Toga app implementing a Fahrenheit to Celsius converter.
## Initial setup
1. Create and activate a virtual environment:
$ python -m venv venv
$ . ./venv/bin/activate
2. Install the demo requirements:
$ pip install -r requirements.txt
### Development details
This demo bakes a pre-compiled version of pyscript into the `server/pyscript`
directory.
It also includes an experimental version of toga-core, toga-web and toga-flask,
packaged as wheels in the `server/wheels` directory.
If any changes are made to the Toga sources or to PyScript, these vendored
resources will need to be updated.
## Web app
The web app is a demo Flask server, serving a web app version of Toga at the
root URL. To run the web demo server:
$ cd server
$ PYTHONPATH=../freedom/src python -m demo
then point your browser at http://localhost:8081/
Enter a value in the "farenheit" input, and click the "calculate" button.
It may take a few seconds for this button to become live; look for the
"Collecting nodes..." entry in the console log.
## Desktop app
To run this app in development mode:
$ briefcase dev
To build and run an app bundle:
$ briefcase run
If you're on an M1 macOS, this will raise an error on first run; if you get this error, run:
$ briefcase package -p app --adhoc-sign
then re-run `briefcase run`
## iOS app
To run this in the iOS simulator, run:
$ briefcase run iOS
To run this in the Android simulator, run:
$ briefcase run android
Note that these builds have extensive requirements that must be installed -
Xcode for iOS, and the Android SDKs for Android. These are multiple gigabyte
downloads. Briefcase will detect when these tools aren't available, and either
prompt you to download them, or perform a download for you. As a result, your
first build may take up to 20 minutes to complete, depending on the speed of
your conection.

Binary file not shown.

View File

@@ -0,0 +1,7 @@
#!/bin/sh
mkdir -p ../server/static/wheels
cd src
unzip ../base-wheel.zip
zip ../../server/static/wheels/freedom-0.0.1-py3-none-any.whl -r freedom*
rm -rf freedom-0.0.1.dist-info

View File

@@ -0,0 +1,49 @@
[build-system]
requires = ["briefcase"]
[tool.briefcase]
project_name = "Freedom Units"
bundle = "org.beeware"
version = "0.0.1"
url = "https://beeware.org"
license = "BSD license"
author = 'Tiberius Yak'
author_email = "tiberius@beeware.org"
[tool.briefcase.app.freedom]
formal_name = "Freedom Units"
description = "A testing app"
sources = ['src/freedom']
requires = [
'../server/static/wheels/toga_core-0.3.0.dev33-py3-none-any.whl',
'../server/static/wheels/travertino-0.1.3-py3-none-any.whl',
]
[tool.briefcase.app.freedom.macOS]
requires = [
'git+https://github.com/beeware/toga.git#egg=toga-cocoa&subdirectory=src/cocoa',
'std-nslog>=1.0.0',
]
[tool.briefcase.app.freedom.linux]
requires = [
'git+https://github.com/beeware/toga.git#egg=toga-gtk&subdirectory=src/gtk',
]
[tool.briefcase.app.freedom.windows]
requires = [
'git+https://github.com/beeware/toga.git#egg=toga-winforms&subdirectory=src/winforms',
]
# Mobile deployments
[tool.briefcase.app.freedom.iOS]
requires = [
'git+https://github.com/beeware/toga.git#egg=toga-iOS&subdirectory=src/iOS',
'std-nslog>=1.0.0',
]
[tool.briefcase.app.freedom.android]
requires = [
'git+https://github.com/beeware/toga.git#egg=toga-android&subdirectory=src/android',
]

View File

@@ -0,0 +1,6 @@
from freedom.app import main
print("IN FREEDOM", main)
if __name__ == '__main__':
main().main_loop()

View File

@@ -0,0 +1,61 @@
import toga
from toga.style.pack import LEFT, RIGHT, COLUMN, ROW, Pack
class FreedomApp(toga.App):
def calculate(self, widget):
try:
self.c_input.value = (float(self.f_input.value) - 32.0) * 5.0 / 9.0
except ValueError:
self.c_input.value = '???'
def startup(self):
self.main_window = toga.MainWindow(title=self.name)
c_box = toga.Box()
f_box = toga.Box()
box = toga.Box()
self.c_input = toga.TextInput(id="c_input", readonly=True)
self.f_input = toga.TextInput(id="f_input")
c_label = toga.Label('Celsius', style=Pack(text_align=LEFT))
f_label = toga.Label('Fahrenheit', style=Pack(text_align=LEFT))
join_label = toga.Label('is equivalent to', style=Pack(text_align=RIGHT))
button = toga.Button('Calculate', id="calculate", on_press=self.calculate)
f_box.add(self.f_input)
f_box.add(f_label)
c_box.add(join_label)
c_box.add(self.c_input)
c_box.add(c_label)
box.add(f_box)
box.add(c_box)
box.add(button)
box.style.update(direction=COLUMN, padding_top=10)
f_box.style.update(direction=ROW, padding=5)
c_box.style.update(direction=ROW, padding=5)
self.c_input.style.update(flex=1)
self.f_input.style.update(flex=1, padding_left=160)
c_label.style.update(width=100, padding_left=10)
f_label.style.update(width=100, padding_left=10)
join_label.style.update(width=150, padding_right=10)
button.style.update(padding=15, flex=1)
self.main_window.content = box
self.main_window.show()
def main():
return FreedomApp('Freedom Units', 'org.beeware.freedom')
if __name__ == '__main__':
main().main_loop()

View File

@@ -0,0 +1,9 @@
setuptools
wheel
check-manifest
briefcase
flask==2.1.1
server/static/wheels/toga_core-0.3.0.dev33-py3-none-any.whl
server/static/wheels/toga_flask-0.3.0.dev33-py3-none-any.whl
server/static/wheels/toga_web-0.3.0.dev33-py3-none-any.whl
server/static/wheels/travertino-0.1.3-py3-none-any.whl

View File

@@ -0,0 +1,13 @@
from flask import Flask
from toga_flask import TogaApp
from freedom import app as freedom
app = Flask(__name__)
app.add_url_rule('/', view_func=TogaApp.as_view("foo", app_module=freedom))
if __name__ == '__main__':
app.run(port=8081, debug=True)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,9 @@
main.toga.window {
margin-top: 5em;
display: flex;
flex-direction: column;
}
div.toga.box {
display: flex;
}

View File

@@ -0,0 +1 @@
Wheels will go here.