async def stand(self, msg): wp = msg.page # Show dealer card 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') await wp.update() await asyncio.sleep(1.1) while True: dealer_hand_value = hand_value(wp.dealer_hand) if dealer_hand_value > 16: break card_dict = jp.Dict(await deal(wp.deck_id)) card = card_dict.cards[0] wp.dealer_hand.append(card) wp.dealer_div.add(Card(src=card.image)) await wp.update() await asyncio.sleep(1.1) 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}' elif dealer_hand_value > player_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}' wp.play_again_btn.remove_class('hidden')
async def blackjack(): 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.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) # 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) 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') return wp
def __init__(self, x, y, **kwargs): super().__init__(x, y, **kwargs) x = np.asarray(x) y = np.asarray(y) m = (len(x) * np.sum(x * y) - np.sum(x) * np.sum(y)) / (len(x) * np.sum(x * x) - np.sum(x)**2) b = (np.sum(y) - m * np.sum(x)) / len(x) s = jp.Dict() # The new series s.type = 'line' s.marker.enabled = False s.enableMouseTracking = False min = float(x.min()) max = float(x.max()) s.data = [[min, m * min + b], [max, m * max + b]] s.name = f'Regression, m: {round(m, 3)}, b: {round(b, 3)}' self.options.series.append(s)
def stock_test(request): wp = jp.WebPage() ticker = request.query_params.get('ticker', 'MSFT') if ticker not in ['AAPL', 'IBM', 'INTC', 'MSFT']: ticker = 'MSFT' data = pd.read_csv( f'https://elimintz.github.io/stocks/{ticker.upper()}.csv') chart = jp.HighStock(a=wp, classes='m-1 p-2 border w-10/12') o = chart.options o.title.text = 'Historical Stock Price' o.legend = {'enabled': True, 'align': 'right', 'layout': 'proximate'} o.rangeSelector.selected = 4 # Set default range to 1 year x = list(data['Date'].map(convert_date)) y = data['Adj Close'].to_list() s = jp.Dict({'name': ticker.upper(), 'data': jp.make_pairs_list(x, y)}) o.series = [s] s.tooltip.valueDecimals = 2 # Price displayed by tooltip will have 2 decimal values return wp
async def hit(self, msg): 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}' else: 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}' 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')
def iris_data(): wp = jp.WebPage(highcharts_theme='gray', title='Iris Dataset', debug=True) jp.Div( text='Iris Dataset', classes= 'text-3xl m-2 p-2 font-medium tracking-wider text-yellow-300 bg-gray-800 text-center', a=wp) d1 = jp.Div(classes='m-2 p-2 border-2', a=wp) chart_list = [] for i, col1 in enumerate(iris.columns[:4]): d2 = jp.Div(classes='flex', a=d1) for j, col2 in enumerate(iris.columns[:4]): if i != j: # Not on the diagonal chart = jp.HighCharts(a=d2, style='width: 300px; height: 300px', classes='flex-grow m-1') chart_list.append(chart.id) chart.chart_list = chart_list chart.on('tooltip', tooltip_formatter) chart.tooltip_y = 85 chart.on('point_click', click_point) chart.col1 = col1 chart.col2 = col2 o = chart.options o.chart.type = 'scatter' o.chart.zoomType = 'xy' o.title.text = '' o.legend.enabled = False o.credits.enabled = False if i < 3 or j < 3 else True # https://api.highcharts.com/highcharts/credits.enabled o.xAxis.title.text = col2 if i == 3 else '' o.yAxis.title.text = col1 if j == 0 else '' o.xAxis.crosshair = o.yAxis.crosshair = True for k, v in iris_species_frames.items(): s = jp.Dict() s.name = k s.allowPointSelect = True # https://api.highcharts.com/highcharts/series.scatter.allowPointSelect s.marker.states.select.radius = 8 s.data = list(zip(v.iloc[:, j], v.iloc[:, i])) o.series.append(s) else: chart = jp.Histogram(list(iris.iloc[:, j]), a=d2, style='width: 300px; height: 300px', classes='flex-grow m-1') o = chart.options o.title.text = '' o.legend.enabled = False o.xAxis[0].title.text = col2 if i == 3 else '' o.xAxis[1].title.text = '' o.yAxis[0].title.text = col1 if j == 0 else '' o.yAxis[1].title.text = '' o.credits.enabled = False if i < 3 or j < 3 else True # Add two grids, first with the data and second with statistics describing the data iris.jp.ag_grid(a=wp, classes='m-2 p-2', style='height: 500px; width: 800px', auto_size=True, theme='ag-theme-balham-dark') iris_stats.jp.ag_grid(a=wp, classes='m-2 p-2 border', style='height: 500px; width: 950px', auto_size=True, theme='ag-theme-material') return wp