Skip to content

Commit

Permalink
added theme to example
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-morel committed Aug 28, 2024
1 parent d10e6ff commit 033d0ae
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 111 deletions.
2 changes: 1 addition & 1 deletion examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def close(self):
print("closed")

def add(self):
self.namespace['number'] += 1
self.namespace["number"] += 1
# self["number"] += 1


Expand Down
1 change: 1 addition & 0 deletions examples/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Todo(Component):
\button text={'mark done' if not todo.done else 'mark undone'} command={toggler(idx)} pos:grid={(1, idx)} pos:sticky='nse'
\button text='remove' command={popper(idx)} pos:grid={(2, idx)} pos:sticky='nse'
"""

todos = [TodoItem("a", True), TodoItem("b", False)]
entry = "Enter todo here"

Expand Down
45 changes: 35 additions & 10 deletions examples/todoapp/todoapp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class Application(Application):
pages = pages
dictionaries = DIR / "dictionaries"
media = DIR / "media"
minsize = (400, 500)
params = dict(
themename="darkly",
minsize=(800, 400),
)
destroy_cache = 5
menu = Menu(
Expand All @@ -49,37 +49,62 @@ class Application(Application):
},
translations="menu",
)
store = (DIR / "store.json", {
"language": "english",
})
store = (
DIR / "store.json",
{
"language": "english",
},
)

def init(self):
self.menu["@preferences/@language"] = {
l: self.dictionaries.get(l).install
for l in self.dictionaries.languages
}
style = self.root.style
self.menu["@preferences/@theme"] = {
t: lambda s=self.set_theme, t=t: s(t) for t in style.theme_names()
}
try:
self.root.style.theme_use(self.store['theme'])
except Exception as e:
log.error(e)
self.menu.update()
self.set_language(self.store['language'])
self.set_language(self.store["language"])
Dictionary.subscribe(self.update_language)

def set_theme(self, theme):
self.root.style.theme_use(theme)
self.store['theme'] = theme
Notification(
"Todos",
_("preferences.success_modified"),
bootstyle="info",
duration=10000,
).show()

def back(self):
self.view.back()

def forward(self):
self.view.forward()

def update_language(self):
self.store['language'] = Dictionary.dictionary.language
self.store["language"] = Dictionary.dictionary.language
self.store.save()
Notification("Todos", _("preferences.success_modified"), bootstyle="info", duration=10000).show()
Notification(
"Todos",
_("preferences.success_modified"),
bootstyle="info",
duration=10000,
).show()

class Layout(Component):
r"""
\frame weight:x='1: 10' weight:y='1: 10, 2: 10'
\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'
\button command={back} image=img:@backward{width: 20} pos:grid=0,0 pos:sticky='w' bootstyle='dark outline'
!if User.is_login()
\label text={f'logged in as: {User.current().name}'} pos:grid=1,0
\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'
\frame:outlet pos:grid=0,1
"""
Expand Down
33 changes: 19 additions & 14 deletions examples/todoapp/todoapp/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ def all(cls):
def create(cls, params):
for raw in data[cls.field()]:
raw = raw.copy()
raw.pop('uuid')
raw.pop("uuid")
if raw == params:
raise cls.DoesExist()
else:
params['uuid'] = str(uuid1())
params["uuid"] = str(uuid1())
data[cls.field()].append(params)
return cls.from_dict(params)

Expand All @@ -64,7 +64,11 @@ def from_dict(cls, params):
return cls(**params)

def save(self):
params = {k: str(v) if isinstance(v, UUID) else v for k, v in vars(self).items() if k in self.__annotations__}
params = {
k: str(v) if isinstance(v, UUID) else v
for k, v in vars(self).items()
if k in self.__annotations__
}
uuid = str(self.uuid)
try:
raw = self._from_uuid(uuid)
Expand All @@ -79,7 +83,7 @@ def save(self):
def delete(self):
uuid = str(self.uuid)
for idx, obj in enumerate(data[self.field()]):
if obj['uuid'] == uuid:
if obj["uuid"] == uuid:
break
else:
raise self.DoesNotExist()
Expand Down Expand Up @@ -114,7 +118,7 @@ def current(cls):
def login(cls, name: str, password: str):
password = sha256(password.encode()).hexdigest()
for raw in data[cls.field()]:
if raw['name'] == name and raw['password'] == password:
if raw["name"] == name and raw["password"] == password:
cls.__current_user__ = cls.from_dict(raw)
return cls.current()
else:
Expand All @@ -123,19 +127,20 @@ def login(cls, name: str, password: str):
@classmethod
@annotate
def signup(cls, name: str, password: str):
user = User.create({
'name': name,
'password': sha256(password.encode()).hexdigest()
})
user = User.create(
{"name": name, "password": sha256(password.encode()).hexdigest()}
)
user.save()
User.__current_user__ = user

def create_todo(self, desc: str, done: bool = False):
return Todo.create({
'author_id': str(self.uuid),
'desc': desc,
'done': done,
})
return Todo.create(
{
"author_id": str(self.uuid),
"desc": desc,
"done": done,
}
)

@classmethod
def is_login(cls):
Expand Down
28 changes: 28 additions & 0 deletions examples/todoapp/todoapp/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
"name": "ama",
"password": "02a6efa44deef545bc56c1aae1e1d9feb53d505bb269a2abd6efe8d8f4e237f2",
"uuid": "eb69e633-6546-11ef-b3f9-28b2bda8b9ca"
},
{
"uuid": "adf19125-6566-11ef-b2d9-28b2bda8b9ca",
"name": "ama",
"password": "4bb7a8b99609b0b8b1d534694bb1f31f129138a2f2a11f8e8702eedbb792922e"
},
{
"uuid": "c83f3c24-6566-11ef-9750-28b2bda8b9ca",
"name": "amas",
"password": "02a6efa44deef545bc56c1aae1e1d9feb53d505bb269a2abd6efe8d8f4e237f2"
}
],
"todos": [
Expand Down Expand Up @@ -47,6 +57,24 @@
"desc": "if ama is colorama then colorama will not be ama still of course, you know: thas is life",
"done": true,
"uuid": "0e4a7b31-6547-11ef-ac34-28b2bda8b9ca"
},
{
"uuid": "cab51d13-6566-11ef-a1a5-28b2bda8b9ca",
"author_id": "c83f3c23-6566-11ef-bb8b-28b2bda8b9ca",
"desc": "Enter you todo her please",
"done": false
},
{
"uuid": "c87360e4-6567-11ef-9ba3-28b2bda8b9ca",
"author_id": "9fd496a6-64a8-11ef-b107-28d244ed9304",
"desc": "I love eating",
"done": false
},
{
"uuid": "daea88ec-6568-11ef-ac06-28b2bda8b9ca",
"author_id": "eb69e633-6546-11ef-b3f9-28b2bda8b9ca",
"desc": "I love it!",
"done": false
}
]
}
1 change: 1 addition & 0 deletions examples/todoapp/todoapp/dictionaries/English.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ menu:
preferences:
__label__: Preferences
language: Language
theme: Theme
preferences:
success_modified: Preferences succesfuly modified
3 changes: 2 additions & 1 deletion examples/todoapp/todoapp/dictionaries/French.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ menu:
preferences:
__label__: Preferences
language: Langue
theme: Theme
preferences:
success_modified: "Preferences linguistiques modifie avec success, (PS: j'aime le francais!)"
success_modified: "Preferences modifie avec success, (PS: j'aime le francais!)"
7 changes: 4 additions & 3 deletions examples/todoapp/todoapp/pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

class Index(Component):
r"""
\frame padding=30 weight:x='1:5'
\frame padding=30 weight:x='1:5' weight:y='0:5'
\frame pos:grid=0,1 padding=5 pos:sticky=''
\label text=[pages.index.welcome] pos:grid=0,0 font="{courier 10 bold}"
\label text=[pages.index.welcome] pos:grid=0,0 font='"Nova Round" 15'
!if User.is_login()
\ctk.button pos:grid=0,2 text=[pages.index.next] pos:sticky='se' command={gt_next}
!if not User.is_login()
Expand All @@ -23,7 +23,8 @@ def gt_next(self):

def gt_login(self):
import taktk
taktk.application("sign", "signin", redirect='todos')

taktk.application("sign", "signin", redirect="todos")

User = User

Expand Down
67 changes: 35 additions & 32 deletions examples/todoapp/todoapp/pages/sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class Signin(Component):
r"""
\frame:form weight:x='0: 1' weight:y='0: 1' padding=20
\frame:form weight:x='0: 1' weight:y='0: 1' padding=50
\frame:form pos:grid=0,0 weight:x='0:1'
\frame pos:grid=0,0 padding=5
\label text=[pages.signin.label.name] pos:grid=0,0 pos:sticky='w'
\entry text={{username}} pos:grid=1,0 width=50 pos:sticky='e'
\frame pos:grid=0,1 padding=5
\label text=[pages.signin.label.password] pos:grid=0,0 pos:sticky='w'
\entry show='*' text={{password}} pos:grid=1,0 width=50 pos:sticky='e'
\frame pos:grid=0,0 padding=20 weight:x='0:1' weight:y='0:1' pos:sticky='nswe'
\label text=[pages.signin.label.name] pos:grid=0,0 pos:sticky='w' font='"Nova Square" 15'
\entry text={{username}} font='"Nova Square" 18' pos:grid=1,0 width=30 pos:sticky='e'
\frame pos:grid=0,1 padding=20 weight:x='0:1' weight:y='0:1' pos:sticky='nswe'
\label text=[pages.signin.label.password] pos:grid=0,0 pos:sticky='w' font='"Nova Square" 15'
\entry show='*' text={{password}} font='"Nova Square" 18' pos:grid=1,0 width=30 pos:sticky='e'
\frame:buttons pos:grid=0,1 pos:sticky='nsew' weight:x='1: 10'
\ctk.button command={gt_signup} text=[pages.signin.gt_signup] pos:grid=0,0 pos:sticky='sw'
\ctk.button command={signin} text=[pages.signin.submit] pos:grid=2,0 pos:sticky='se'
Expand All @@ -27,51 +27,47 @@ def __init__(self, redirect):
password = ""

def signin(self):
name = self['username']
password = self['password']
name = self["username"]
password = self["password"]
try:
user = User.login(name, password)
except User.DoesNotExist:
Notification(
"Todos",
"Wrong login: please verify credentials and re-enter",
bootstyle='danger',
bootstyle="danger",
source=None,
duration=10000,
).show()
else:
Notification(
"Todos",
"Signin successful",
bootstyle='info',
bootstyle="info",
source="signin-page",
duration=5000,
).show()
taktk.application.view.url(self.redirect)

def gt_signup(self):
taktk.application.view.url('sign#signup')

taktk.application.view.url("sign#signup")


def signin(store, /, redirect='todos'):
def signin(store, /, redirect="todos"):
return Signin(redirect=redirect)






class Signup(Component):
r"""
\frame:form weight:x='0: 1' weight:y='0: 1' padding=20
\frame:form pos:grid=0,0 weight:x='0:1'
\frame pos:grid=0,0 padding=5
\label text=[pages.signin.label.name] pos:grid=0,0 pos:sticky='w'
\entry text={{username}} pos:grid=1,0 width=50 pos:sticky='e'
\frame pos:grid=0,1 padding=5
\label text=[pages.signin.label.password] pos:grid=0,0 pos:sticky='w'
\entry show='*' text={{password}} pos:grid=1,0 width=50 pos:sticky='e'
\frame pos:grid=0,0 padding=20 weight:x='0:1' weight:y='0:1' pos:sticky='nswe'
\label text=[pages.signin.label.name] pos:grid=0,0 pos:sticky='w' font='"Nova Square" 15'
\entry text={{username}} font='"Nova Square" 18' pos:grid=1,0 width=30 pos:sticky='e'
\frame pos:grid=0,1 padding=20 weight:x='0:1' weight:y='0:1' pos:sticky='nswe'
\label text=[pages.signin.label.password] pos:grid=0,0 pos:sticky='w' font='"Nova Square" 15'
\entry show='*' text={{password}} font='"Nova Square" 18' pos:grid=1,0 width=30 pos:sticky='e'
\entry show='*' text={{password2}} font='"Nova Square" 18' pos:grid=1,1 width=30 pos:sticky='e'
\frame:buttons pos:grid=0,1 pos:sticky='nsew' weight:x='1: 10'
\ctk.button command={gt_signin} text=[pages.signin.gt_signin] pos:grid=0,0 pos:sticky='sw'
\ctk.button command={signup} text=[pages.signin.submit] pos:grid=2,0 pos:sticky='se'
Expand All @@ -83,35 +79,42 @@ def __init__(self, redirect):

username = ""
password = ""
password2 = ""

def signup(self):
name = self['username']
password = self['password']
name = self["username"]
password = self["password"]
if self["password"] != self["password2"]:
Notification(
"Todos",
"Unmatching password",
duration=5000,
bootstyle="danger",
).show()
return
try:
user = User.signup(name, password)
except User.DoesExist:
Notification(
"Todos",
"User already exists",
bootstyle='danger',
bootstyle="danger",
source=None,
duration=10000,
).show()
else:
Notification(
"Todos",
"Signup successful",
bootstyle='info',
bootstyle="info",
source="signin-page",
duration=5000,
).show()
taktk.application.view.url(self.redirect)

def gt_signin(self):
taktk.application.view.url('sign#signin')

taktk.application.view.url("sign#signin")


def signup(store, /, redirect='todos'):
def signup(store, /, redirect="todos"):
return Signup(redirect=redirect)

Loading

0 comments on commit 033d0ae

Please sign in to comment.