Skip to content

Commit

Permalink
tested routing patterns, improved example, fixed bug when using inlin…
Browse files Browse the repository at this point in the history
…e lambda in expression attribute
  • Loading branch information
ken-morel committed Aug 28, 2024
1 parent 033d0ae commit 6be1bfe
Show file tree
Hide file tree
Showing 32 changed files with 13,081 additions and 6,228 deletions.
11 changes: 8 additions & 3 deletions examples/todoapp/todoapp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Application(Application):
DIR / "store.json",
{
"language": "english",
"theme": "darkly",
},
)

Expand Down Expand Up @@ -102,10 +103,11 @@ def update_language(self):
class Layout(Component):
r"""
\frame weight:x='0: 10' weight:y='1: 10, 2: 10'
\frame padding=5 weight:y='2:10' weight:x='2:10' pos:grid=0,0 pos:sticky='nsew'
\frame padding=5 weight:y='2:10' weight:x='3:10' pos:grid=0,0 pos:sticky='nsew'
\button command={back} image=img:@backward{width: 20} pos:grid=0,0 pos:sticky='w' bootstyle='dark outline'
\label text={f'logged in as: {User.current().name}' if User.current() else "not logged in!"} pos:grid=1,0
\button command={forward} image=img:@forward{width: 20} pos:grid=3,0 pos:sticky='e' bootstyle='dark outline'
\button command={gt_users} image=img:@users-between-lines{height: 20} pos:grid=1,0 pos:sticky='w' bootstyle='dark outline'
\label text={f'logged in as: {User.current().name}' if User.current() else "not logged in!"} pos:grid=2,0
\button command={forward} image=img:@forward{width: 20} pos:grid=4,0 pos:sticky='e' bootstyle='dark outline'
\frame:outlet pos:grid=0,1
"""

Expand All @@ -121,4 +123,7 @@ def back(self):
def forward(self):
self.app.forward()

def gt_users(self):
self.app("users")

User = User
39 changes: 20 additions & 19 deletions examples/todoapp/todoapp/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,10 @@
from dataclasses import dataclass
from uuid import UUID, uuid1
from hashlib import sha256
import taktk

DIR = Path(__file__).parent

data = Store(
DIR / "data.json",
{
"users": [],
"todos": [],
},
)


class Model:
@classmethod
def __init_subclass__(cls):
Expand All @@ -28,24 +20,24 @@ def __init_subclass__(cls):

@classmethod
def all(cls):
return list(map(cls.from_dict, data[cls.field()]))
return list(map(cls.from_dict, store[cls.field()]))

@classmethod
def create(cls, params):
for raw in data[cls.field()]:
for raw in store[cls.field()]:
raw = raw.copy()
raw.pop("uuid")
if raw == params:
raise cls.DoesExist()
else:
params["uuid"] = str(uuid1())
data[cls.field()].append(params)
store[cls.field()].append(params)
return cls.from_dict(params)

@classmethod
def _from_uuid(cls, uuid):
uuid = str(uuid)
for raw in data[cls.field()]:
for raw in store[cls.field()]:
if raw["uuid"] == uuid:
return raw
else:
Expand Down Expand Up @@ -77,18 +69,18 @@ def save(self):
else:
raw.update(params)
finally:
data.save()
store.save()
return self

def delete(self):
uuid = str(self.uuid)
for idx, obj in enumerate(data[self.field()]):
for idx, obj in enumerate(store[self.field()]):
if obj["uuid"] == uuid:
break
else:
raise self.DoesNotExist()
data[self.field()].pop(idx)
data.save()
store[self.field()].pop(idx)
store.save()
return None

class Exception(ValueError):
Expand Down Expand Up @@ -117,7 +109,7 @@ def current(cls):
@annotate
def login(cls, name: str, password: str):
password = sha256(password.encode()).hexdigest()
for raw in data[cls.field()]:
for raw in store[cls.field()]:
if raw["name"] == name and raw["password"] == password:
cls.__current_user__ = cls.from_dict(raw)
return cls.current()
Expand Down Expand Up @@ -160,9 +152,18 @@ def for_user(cls, user: User):
return list(
filter(
lambda t, u=user: t.has_user(u),
map(Todo.from_dict, data[cls.field()]),
map(Todo.from_dict, store[cls.field()]),
)
)

def has_user(self, user: User):
return str(self.author_id) == str(user.uuid)


@taktk.on_create
def init(app):
global store
store = app.store.for_page(__name__, {
"users": [],
"todos": [],
})
80 changes: 0 additions & 80 deletions examples/todoapp/todoapp/data.json

This file was deleted.

2 changes: 2 additions & 0 deletions examples/todoapp/todoapp/dictionaries/English.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pages:
submit: Login >
gt_signup: < No account, signup
gt_signin: < Already have an account? signin
users:
view: view >
nav:
back: < back
next: next >
Expand Down
2 changes: 2 additions & 0 deletions examples/todoapp/todoapp/dictionaries/French.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pages:
submit: Proceder >
gt_signin: < avez vous un compte? se connecter
gt_signup: < Pas de compte?, s'enregistrer
users:
view: visiter >
nav:
back: < precedant
next: suivant >
Expand Down
Binary file modified examples/todoapp/todoapp/media/img/backward.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/todoapp/todoapp/media/img/backword.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/todoapp/todoapp/media/img/forward.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions examples/todoapp/todoapp/media/img/rmbg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def rmbg(
infile: Path,
outfile: Path = None,
color: tuple[int, int, int] = (255, 255, 255),
replace: tuple[int, int, int, int] = (0, 0, 0, 0),
tolerance: int = 10,
):
from PIL import Image
Expand All @@ -20,9 +21,9 @@ def rmbg(
for y in range(height):
pixel = image.getpixel((x, y))[:3]
if pixel_difference(pixel, color) < tolerance:
image.putpixel((x, y), pixel + (0,))
image.putpixel((x, y), replace)
image.save(outfile or infile)


for x in Path(".").glob("*.png"):
rmbg(x)
rmbg(x, color=(0, 0, 0), replace=(50, 50, 70))
Binary file added examples/todoapp/todoapp/media/img/users-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/todoapp/todoapp/media/img/users-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/todoapp/todoapp/media/img/users-between-lines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/todoapp/todoapp/media/img/users.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/todoapp/todoapp/media/img/users.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions examples/todoapp/todoapp/pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from . import todos
from taktk.notification import Notification
from ..admin import User
from taktk.page import register_urlpattern


class Index(Component):
Expand Down Expand Up @@ -31,3 +32,11 @@ def gt_login(self):

def handle(store, /, **params):
return Index()


@register_urlpattern(
r"[\da-f]{8}\-[\da-f]{4}\-[\da-f]{4}\-[\da-f]{4}\-[\da-f]{12}",
position=0,
)
def user(uuid):
return User.from_uuid(uuid)
17 changes: 17 additions & 0 deletions examples/todoapp/todoapp/pages/_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from taktk.component import Component
import taktk


class User(Component):
r"""
\frame weight:x='0: 10, 1: 2, 2: 10' padding=50
\label text={user.name} pos:grid=0,0 pos:sticky='w' font='arial 20'
"""

def __init__(self, user):
self.user = user
super().__init__()


def handle(store, user):
return User(user=user)
Empty file.
25 changes: 25 additions & 0 deletions examples/todoapp/todoapp/pages/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from taktk.component import Component
from ..admin import User
import taktk


class Users(Component):
r"""
\frame weight:x='0: 10, 1: 2, 2: 10' padding=50
!enum users:(y, sub_users)
!enum sub_users:(x, user)
!if user is not None
\frame pos:grid={(x * 2, y)} pos:sticky='nswe' padding=20 weight:x='1: 10' borderwidth=1 relief='solid'
\label text={user.name} pos:grid=0,0 pos:sticky='w' font='arial 20'
\button bootstyle='info' text=[pages.users.view] pos:grid=2,0 command={lambda u=user.uuid, v=visit: v(u)}
"""
def init(self):
users = User.all()
self['users'] = [(users[x], users[x + 1] if x + 1 < len(users) else None) for x in range(0, len(users), 2)]

def visit(self, uuid):
taktk.application(str(uuid))


def handle(store):
return Users()
3 changes: 0 additions & 3 deletions examples/todoapp/todoapp/settings.json

This file was deleted.

68 changes: 63 additions & 5 deletions examples/todoapp/todoapp/store.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,66 @@
{
"language": "French",
"__pageStore_todoapp.pages.todos__": {
"entry:ama": "I love enterring my todos here!",
"entry:ken-morel": "Noo!"
"theme": "journal",
"__pageStore_todoapp.admin__": {
"users": [
{
"uuid": "d23acc8d-6577-11ef-82f6-28b2bda8b9ca",
"name": "ama",
"password": "02a6efa44deef545bc56c1aae1e1d9feb53d505bb269a2abd6efe8d8f4e237f2"
},
{
"uuid": "3a779793-6592-11ef-8e09-28d244ed9304",
"name": "ken-morel",
"password": "02a6efa44deef545bc56c1aae1e1d9feb53d505bb269a2abd6efe8d8f4e237f2"
}
],
"todos": [
{
"uuid": "d73a63ff-6577-11ef-b372-28b2bda8b9ca",
"author_id": "d23acc8c-6577-11ef-9b00-28b2bda8b9ca",
"desc": "colorama",
"done": false
},
{
"uuid": "db640900-6577-11ef-976c-28b2bda8b9ca",
"author_id": "d23acc8c-6577-11ef-9b00-28b2bda8b9ca",
"desc": "I love colorama",
"done": false
},
{
"uuid": "dec7d133-6577-11ef-9190-28b2bda8b9ca",
"author_id": "d23acc8c-6577-11ef-9b00-28b2bda8b9ca",
"desc": "WHat is?",
"done": false
},
{
"uuid": "e986fad6-6577-11ef-8973-28b2bda8b9ca",
"author_id": "d23acc8c-6577-11ef-9b00-28b2bda8b9ca",
"desc": "I love it!",
"done": false
},
{
"uuid": "ed72a4ba-6577-11ef-ad77-28b2bda8b9ca",
"author_id": "d23acc8c-6577-11ef-9b00-28b2bda8b9ca",
"desc": "I love eating",
"done": false
},
{
"uuid": "f12c3ef2-6577-11ef-99d3-28b2bda8b9ca",
"author_id": "d23acc8c-6577-11ef-9b00-28b2bda8b9ca",
"desc": "You do so too!",
"done": false
},
{
"uuid": "f13382e0-6593-11ef-a86c-28d244ed9304",
"author_id": "d23acc8d-6577-11ef-82f6-28b2bda8b9ca",
"desc": "kenpd",
"done": true
}
]
},
"theme": "sandstone"
}
"__pageStore_todoapp.pages.todos__": {
"entry:ama": "",
"entry:ken-morel": "Notez ici"
}
}
Loading

0 comments on commit 6be1bfe

Please sign in to comment.