Skip to content

Commit

Permalink
Merge pull request #499 from platinops/apply-black-code-formatting
Browse files Browse the repository at this point in the history
Apply default black code formatting
  • Loading branch information
WolfgangFahl authored Sep 6, 2022
2 parents c7d9fe8 + 1a24f85 commit 7963a2f
Show file tree
Hide file tree
Showing 76 changed files with 8,404 additions and 3,454 deletions.
4 changes: 3 additions & 1 deletion docker/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import justpy as jp


def hello_world():
wp = jp.WebPage()
jp.Hello(a=wp)
return wp

jp.justpy(hello_world)

jp.justpy(hello_world)
27 changes: 15 additions & 12 deletions examples/basedemo.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
'''
"""
Created on 2022-09-07
@author: wf
'''
"""
import argparse
import socket
import typing


class Demo(object):
'''
"""
Base class for justpy demos to allow selecting host and port from commandline
'''
testmode=False
"""

testmode = False

def __init__(self, name:str,wp:typing.Callable,**kwargs):
'''
def __init__(self, name: str, wp: typing.Callable, **kwargs):
"""
Constructor
Args:
name(str): the name of the demo
host(int): the port to runt he demo at (defaut:8000)
wp(callable): the webpage callback
**kwargs: further keyword arguments
'''
"""
# make sure Demo code may be tested without automatically starting
if Demo.testmode:
return
parser = argparse.ArgumentParser(description=name)
parser.add_argument('--host',default=socket.getfqdn())
parser.add_argument('--port',type=int,default=8000)
parser.add_argument("--host", default=socket.getfqdn())
parser.add_argument("--port", type=int, default=8000)
args = parser.parse_args()
import justpy as jp
jp.justpy(wp,host=args.host,port=args.port,**kwargs)

jp.justpy(wp, host=args.host, port=args.port, **kwargs)
144 changes: 82 additions & 62 deletions examples/blackjack.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,68 @@

card_size = 0.5
card_width = int(226 * card_size)
card_height = int(card_width * 314/226)
card_height = int(card_width * 314 / 226)
button_classes = """
inline-flex items-center px-6 py-6 border border-gray-700 text-2xl leading-6 font-bold rounded-md text-gray-700
bg-white hover:text-gray-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue active:text-gray-800
active:bg-gray-50 transition ease-in-out duration-150
"""
div_classes = 'items-center px-6 py-6 border border-gray-700 text-3xl leading-6 font-bold rounded-md text-gray-700; bg-white '
div_classes = "items-center px-6 py-6 border border-gray-700 text-3xl leading-6 font-bold rounded-md text-gray-700; bg-white "


class Card(jp.Img):
'''
Card component: an extended image with predefined width and height and a transition
'''
"""
Card component: an extended image with predefined width and height and a transition
"""

def __init__(self, **kwargs):
'''
"""
constructor
'''
"""
super().__init__(**kwargs)
self.width = card_width
self.height = card_height
self.set_class('ml-2')
self.set_class("ml-2")

self.transition = {
'load': 'transition origin-left ease-out duration-1000',
'load_start': 'transform scale-x-0',
'load_end': 'transform scale-x-100',
'enter': 'transition origin-left ease-out duration-1000',
'enter_start': 'transform scale-x-0',
'enter_end': 'transform scale-x-100'
}
"load": "transition origin-left ease-out duration-1000",
"load_start": "transform scale-x-0",
"load_end": "transform scale-x-100",
"enter": "transition origin-left ease-out duration-1000",
"enter_start": "transform scale-x-0",
"enter_end": "transform scale-x-100",
}


async def create_deck():
'''
"""
get a new deck
'''
deck = await jp.get('https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1')
return deck['deck_id']
"""
deck = await jp.get("https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1")
return deck["deck_id"]


async def deal(deck_id, count=1):
'''
"""
deal
'''
"""
try:
cards = await jp.get(f'https://deckofcardsapi.com/api/deck/{deck_id}/draw/?count={count}')
cards = await jp.get(
f"https://deckofcardsapi.com/api/deck/{deck_id}/draw/?count={count}"
)
except:
print('error in deal')
print("error in deal")
return cards


def hand_value(hand):
'''
"""
calculate the hand value
'''
"""
value = 0
aces = 0
for card in hand:
if card.value == 'ACE':
if card.value == "ACE":
aces += 1
value += 1
else:
Expand All @@ -82,39 +84,39 @@ def hand_value(hand):


async def hit(self, msg):
'''
"""
react on hit button
'''
"""
wp = msg.page
card_dict = jp.Dict(await deal(wp.deck_id))
card = card_dict.cards[0]
wp.player_hand.append(card)
wp.player_div.add(Card(src=card.image))
player_hand_value = hand_value(wp.player_hand)
if player_hand_value < 22:
wp.hand_value_div.text=f'Hand value: {player_hand_value}'
wp.hand_value_div.text = f"Hand value: {player_hand_value}"
else:
wp.hand_value_div.text=f'YOU HAVE BUSTED, Hand value: {player_hand_value}'
wp.hand_value_div.text = f"YOU HAVE BUSTED, Hand value: {player_hand_value}"
dealer_hand_value = hand_value(wp.dealer_hand)
result_div = jp.Div(classes=div_classes, a=wp.outer_div)
result_div.text = f'YOU LOST, Your hand: {player_hand_value}, Dealer\'s hand: {dealer_hand_value}'
result_div.text = f"YOU LOST, Your hand: {player_hand_value}, Dealer's hand: {dealer_hand_value}"
for btn in [wp.stand_btn, wp.hit_btn]:
btn.disabled = True
btn.set_classes('cursor-not-allowed bg-gray-200 opacity-50')
wp.play_again_btn.remove_class('hidden')
btn.set_classes("cursor-not-allowed bg-gray-200 opacity-50")
wp.play_again_btn.remove_class("hidden")


async def stand(self, msg):
'''
"""
react on stand button
'''
"""
wp = msg.page
# Show dealer card
wp.card_back.set_class('hidden')
wp.down_card.remove_class('hidden')
wp.card_back.set_class("hidden")
wp.down_card.remove_class("hidden")
for btn in [wp.stand_btn, wp.hit_btn]:
btn.disabled = True
btn.set_classes('cursor-not-allowed bg-gray-200 opacity-50')
btn.set_classes("cursor-not-allowed bg-gray-200 opacity-50")
await wp.update()
await asyncio.sleep(1.1)

Expand All @@ -131,51 +133,69 @@ async def stand(self, msg):
player_hand_value = hand_value(wp.player_hand)
result_div = jp.Div(classes=div_classes, a=wp.outer_div)
if (dealer_hand_value > 21) or (dealer_hand_value < player_hand_value):
result_div.text = f'YOU WON, Your hand: {player_hand_value}, Dealer\'s hand: {dealer_hand_value}'
result_div.text = f"YOU WON, Your hand: {player_hand_value}, Dealer's hand: {dealer_hand_value}"
elif dealer_hand_value > player_hand_value:
result_div.text=f'YOU LOST, Your hand: {player_hand_value}, Dealer\'s hand: {dealer_hand_value}'
result_div.text = f"YOU LOST, Your hand: {player_hand_value}, Dealer's hand: {dealer_hand_value}"
else:
result_div.text = f'IT IS A DRAW, Your hand: {player_hand_value}, Dealer\'s hand: {dealer_hand_value}'
result_div.text = f"IT IS A DRAW, Your hand: {player_hand_value}, Dealer's hand: {dealer_hand_value}"

wp.play_again_btn.remove_class('hidden')
wp.play_again_btn.remove_class("hidden")


async def play_again(self, msg):
'''
"""
react on play again button
'''
"""
wp = msg.page
await wp.reload()


async def blackjack():
'''
"""
the async web page to serve
'''
"""
wp = jp.WebPage()
wp.outer_div = jp.Div(classes='container mx-auto px-4 sm:px-6 lg:px-8 space-y-5', a=wp)
wp.outer_div = jp.Div(
classes="container mx-auto px-4 sm:px-6 lg:px-8 space-y-5", a=wp
)
wp.deck_id = await create_deck()
# Deal initial four cards
cards = jp.Dict(await deal(wp.deck_id, 4))
wp.player_hand = [cards.cards[0], cards.cards[2]]
wp.dealer_hand = [cards.cards[1], cards.cards[3]]
jp.Div(text='Blackjack Demo', a=wp.outer_div, classes='m-2 p-4 text-3xl font-bold leading-7 text-gray-900 sm:leading-9 sm:truncate')
wp.dealer_div = jp.Div(classes='flex flex-wrap m-2', a=wp.outer_div)
jp.Div(
text="Blackjack Demo",
a=wp.outer_div,
classes="m-2 p-4 text-3xl font-bold leading-7 text-gray-900 sm:leading-9 sm:truncate",
)
wp.dealer_div = jp.Div(classes="flex flex-wrap m-2", a=wp.outer_div)
# Image of back of card
wp.card_back = Card(src='https://raw.githubusercontent.com/elimintz/elimintz.github.io/master/card_back.png', a=wp.dealer_div)
wp.down_card = Card(src=wp.dealer_hand[0].image, a=wp.dealer_div, classes='hidden')
Card(src=wp.dealer_hand[1].image, a=wp.dealer_div, style='transition-delay: 1000ms')
wp.player_div = jp.Div(classes='flex flex-wrap m-2', a=wp.outer_div)
wp.card_back = Card(
src="https://raw.githubusercontent.com/elimintz/elimintz.github.io/master/card_back.png",
a=wp.dealer_div,
)
wp.down_card = Card(src=wp.dealer_hand[0].image, a=wp.dealer_div, classes="hidden")
Card(src=wp.dealer_hand[1].image, a=wp.dealer_div, style="transition-delay: 1000ms")
wp.player_div = jp.Div(classes="flex flex-wrap m-2", a=wp.outer_div)
for card in wp.player_hand:
Card(src=card.image, a=wp.player_div, style='transition-delay: 2000ms')
button_div = jp.Div(classes='flex m-2 space-x-6', a=wp.outer_div)
wp.stand_btn = jp.Button(text='Stand', a=button_div, classes=button_classes, click=stand)
wp.hit_btn = jp.Button(text='Hit', a=button_div, classes=button_classes, click=hit)
wp.play_again_btn = jp.Button(text='Play Again', a=button_div, classes=button_classes, click=play_again)
wp.play_again_btn.set_class('hidden')
wp.hand_value_div = jp.Div(text=f'Hand value: {hand_value(wp.player_hand)}', a=wp.outer_div, classes='text-2xl')
Card(src=card.image, a=wp.player_div, style="transition-delay: 2000ms")
button_div = jp.Div(classes="flex m-2 space-x-6", a=wp.outer_div)
wp.stand_btn = jp.Button(
text="Stand", a=button_div, classes=button_classes, click=stand
)
wp.hit_btn = jp.Button(text="Hit", a=button_div, classes=button_classes, click=hit)
wp.play_again_btn = jp.Button(
text="Play Again", a=button_div, classes=button_classes, click=play_again
)
wp.play_again_btn.set_class("hidden")
wp.hand_value_div = jp.Div(
text=f"Hand value: {hand_value(wp.player_hand)}",
a=wp.outer_div,
classes="text-2xl",
)
return wp


from examples.basedemo import Demo
Demo('blackjack demo',blackjack)
from examples.basedemo import Demo

Demo("blackjack demo", blackjack)
9 changes: 6 additions & 3 deletions examples/blog/reactivity/todo_app1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Justpy Tutorial demo todo_app1 from docs/blog/reactivity.md
import justpy as jp

todos = ['Go shopping', 'Learn JustPy', 'Do errands']
todos = ["Go shopping", "Learn JustPy", "Do errands"]


def todo_app1():
wp = jp.WebPage(tailwind=False)
Expand All @@ -10,6 +11,8 @@ def todo_app1():
jp.Li(text=todo, a=ol)
return wp


# initialize the demo
from examples.basedemo import Demo
Demo ("todo_app1",todo_app1)
from examples.basedemo import Demo

Demo("todo_app1", todo_app1)
14 changes: 8 additions & 6 deletions examples/blog/reactivity/todo_app2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@


def todo_app2():
todos = ['Buy groceries', 'Learn JustPy', 'Do errands']
todos = ["Buy groceries", "Learn JustPy", "Do errands"]
wp = jp.WebPage(tailwind=False)
ol = jp.Ol(a=wp)
for todo in todos:
jp.Li(text=todo, a=ol)
task = jp.Input(placeholder='Enter task', a=wp)
task = jp.Input(placeholder="Enter task", a=wp)

def add_task(self, msg):
todos.append(task.value)
task.value = ''
task.value = ""
ol.delete_components()
for todo in todos:
jp.Li(text=todo, a=ol)

jp.Button(text='Add Task', a=wp, click=add_task, style='margin-left: 10px')
jp.Button(text="Add Task", a=wp, click=add_task, style="margin-left: 10px")

return wp


# initialize the demo
from examples.basedemo import Demo
Demo ("todo_app2",todo_app2)
from examples.basedemo import Demo

Demo("todo_app2", todo_app2)
15 changes: 8 additions & 7 deletions examples/blog/reactivity/todo_app3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


class TodoList(jp.Ol):

def __init__(self, **kwargs):
self.todos = []
super().__init__(**kwargs)
Expand All @@ -16,17 +15,19 @@ def react(self, data):

def todo_app3():
wp = jp.WebPage(tailwind=False)
todo_list = TodoList(a=wp, todos=['Buy groceries', 'Learn JustPy', 'Do errands'])
task = jp.Input(placeholder='Enter task', a=wp)
todo_list = TodoList(a=wp, todos=["Buy groceries", "Learn JustPy", "Do errands"])
task = jp.Input(placeholder="Enter task", a=wp)

def add_task(self, msg):
todo_list.todos.append(task.value)
task.value = ''
task.value = ""

jp.Button(text='Add Task', a=wp, click=add_task, style='margin-left: 10px')
jp.Button(text="Add Task", a=wp, click=add_task, style="margin-left: 10px")

return wp


# initialize the demo
from examples.basedemo import Demo
Demo ("todo_app3",todo_app3)
from examples.basedemo import Demo

Demo("todo_app3", todo_app3)
Loading

0 comments on commit 7963a2f

Please sign in to comment.