Update app.py
Browse files
app.py
CHANGED
|
@@ -624,7 +624,7 @@ def server(input, output, session):
|
|
| 624 |
threshold = 24
|
| 625 |
scaling_factor = 1.5 # Adjust this factor as needed
|
| 626 |
|
| 627 |
-
|
| 628 |
for i, (_, team_row) in enumerate(df_pandas.head(10).iterrows()):
|
| 629 |
team = team_row[f'team']
|
| 630 |
player = team_row[f'name']
|
|
@@ -636,98 +636,111 @@ def server(input, output, session):
|
|
| 636 |
|
| 637 |
# Create a subplot in the GridSpec layout
|
| 638 |
ax = fig.add_subplot(gs[row, col])
|
| 639 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 640 |
|
| 641 |
# Plot the team logo
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
|
| 654 |
-
# Open the PNG with PIL
|
| 655 |
-
img = Image.open(BytesIO(png_data))
|
| 656 |
-
# img = img.convert("LA")
|
| 657 |
-
|
| 658 |
-
ax.imshow(img, extent=[-0.81, -0.63, 0.05, 0.95],aspect=0.2)
|
| 659 |
ax.axis('off')
|
| 660 |
-
|
| 661 |
# Add the rank number to the left of the logo, italicized
|
| 662 |
-
ax.text(0.05, 0.5, f'{i + 1}', transform=ax.transAxes, ha='center', va='center',
|
|
|
|
|
|
|
| 663 |
adjusted_fontsize = max(20, base_size - max(0, (len(player) - threshold) * scaling_factor))
|
| 664 |
-
|
| 665 |
-
#
|
| 666 |
-
ax.text(0.27, 0.5, f'{player}', transform=ax.transAxes, ha='left', va='center',
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
#
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
# lw=2, zorder=10, alpha=0.1, facecolor=mlb_team_colors_abb[team],
|
| 675 |
-
# ec=None))
|
| 676 |
-
|
| 677 |
-
# ax.add_patch(Rectangle((-1.1, 0.02), 2.2, 0.96, fill=False,
|
| 678 |
-
# lw=4, zorder=11, alpha=1, facecolor=None,
|
| 679 |
-
# ec=mlb_team_secondary_colors_abb[team]))
|
| 680 |
-
|
| 681 |
-
|
| 682 |
# Adjust the spacing between subplots to place them on the borders
|
| 683 |
ax_top = fig.add_subplot(gs[0, :])
|
| 684 |
ax_bot = fig.add_subplot(gs[-1, :])
|
| 685 |
ax_left = fig.add_subplot(gs[:, 0])
|
| 686 |
ax_right = fig.add_subplot(gs[:, -1])
|
| 687 |
-
|
| 688 |
-
|
| 689 |
ax_top.axis('off')
|
| 690 |
ax_bot.axis('off')
|
| 691 |
ax_left.axis('off')
|
| 692 |
ax_right.axis('off')
|
| 693 |
-
|
| 694 |
ax_bot.text(s='By: @TJStats', x=0.1, y=0.5, fontsize=24, ha='left')
|
| 695 |
ax_bot.text(s='Data: MLB', x=0.9, y=0.5, fontsize=24, ha='right')
|
| 696 |
-
ax_bot.text(s=f"2025 MLB Season\n{datetime.datetime.today().strftime('%Y-%m-%d')}",
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
ax_stat_select = fig.add_subplot(gs[1, 1])
|
| 701 |
ax_stat_select.set_xlim(0, 2.5)
|
| 702 |
-
|
| 703 |
-
import numpy as np
|
| 704 |
-
from PIL import Image
|
| 705 |
blank_image = np.ones((100, 100, 3), dtype=np.uint8) * 255
|
| 706 |
-
ax_stat_select.imshow(blank_image, extent=[-0.76, -0.58, 0.05, 0.95],aspect=0.2)
|
| 707 |
-
|
| 708 |
-
ax_stat_select.text(0.90*2.5, 0.0, f'{stat_label}', ha='center', va='bottom',
|
|
|
|
| 709 |
ax_stat_select.axis('off')
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
ax_watermark = fig.add_subplot(gs[1:-1,1:-1],zorder=-1)
|
| 719 |
-
# Hide axes ticks and labels
|
| 720 |
ax_watermark.set_xticks([])
|
| 721 |
ax_watermark.set_yticks([])
|
| 722 |
-
ax_watermark.set_frame_on(False)
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
@output
|
| 732 |
@render.plot
|
| 733 |
@reactive.event(input.generate_plot, ignore_none=True)
|
|
|
|
| 624 |
threshold = 24
|
| 625 |
scaling_factor = 1.5 # Adjust this factor as needed
|
| 626 |
|
| 627 |
+
# Iterate over the top 10 players in the sorted dataframe
|
| 628 |
for i, (_, team_row) in enumerate(df_pandas.head(10).iterrows()):
|
| 629 |
team = team_row[f'team']
|
| 630 |
player = team_row[f'name']
|
|
|
|
| 636 |
|
| 637 |
# Create a subplot in the GridSpec layout
|
| 638 |
ax = fig.add_subplot(gs[row, col])
|
| 639 |
+
|
| 640 |
+
# Plot the player picture
|
| 641 |
+
try:
|
| 642 |
+
img = plt.imread(logo_url)
|
| 643 |
+
ax.set_xlim(-1.25, 1.25)
|
| 644 |
+
ax.imshow(img, extent=[-1.02, -0.84, 0.05, 0.95], aspect=0.2)
|
| 645 |
+
except Exception as e:
|
| 646 |
+
print(f"Failed to load player image from {logo_url}: {e}")
|
| 647 |
|
| 648 |
# Plot the team logo
|
| 649 |
+
try:
|
| 650 |
+
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'}
|
| 651 |
+
response = requests.get(team_url, headers=headers, timeout=10)
|
| 652 |
+
|
| 653 |
+
# Validate response
|
| 654 |
+
if response.status_code != 200:
|
| 655 |
+
print(f"Failed to fetch {team_url}: HTTP {response.status_code}")
|
| 656 |
+
raise ValueError(f"HTTP {response.status_code}")
|
| 657 |
+
|
| 658 |
+
if not response.content:
|
| 659 |
+
print(f"Empty response for {team_url}")
|
| 660 |
+
raise ValueError("Empty response")
|
| 661 |
+
|
| 662 |
+
# Check if it's actually SVG or already a raster format
|
| 663 |
+
content_type = response.headers.get('content-type', '').lower()
|
| 664 |
+
|
| 665 |
+
if 'svg' in content_type or team_url.lower().endswith('.svg'):
|
| 666 |
+
# Convert SVG to PNG
|
| 667 |
+
png_data = cairosvg.svg2png(
|
| 668 |
+
bytestring=response.content,
|
| 669 |
+
output_width=400,
|
| 670 |
+
output_height=400
|
| 671 |
+
)
|
| 672 |
+
img = Image.open(BytesIO(png_data))
|
| 673 |
+
else:
|
| 674 |
+
# Already a raster format
|
| 675 |
+
img = Image.open(BytesIO(response.content))
|
| 676 |
+
|
| 677 |
+
ax.imshow(img, extent=[-0.81, -0.63, 0.05, 0.95], aspect=0.2)
|
| 678 |
+
|
| 679 |
+
except Exception as e:
|
| 680 |
+
print(f"Failed to process team logo from {team_url}: {e}")
|
| 681 |
+
# Create blank placeholder
|
| 682 |
+
blank_image = np.ones((100, 100, 3), dtype=np.uint8) * 240
|
| 683 |
+
img = Image.fromarray(blank_image)
|
| 684 |
+
ax.imshow(img, extent=[-0.81, -0.63, 0.05, 0.95], aspect=0.2)
|
| 685 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 686 |
ax.axis('off')
|
| 687 |
+
|
| 688 |
# Add the rank number to the left of the logo, italicized
|
| 689 |
+
ax.text(0.05, 0.5, f'{i + 1}', transform=ax.transAxes, ha='center', va='center',
|
| 690 |
+
fontsize=36, style='italic')
|
| 691 |
+
|
| 692 |
adjusted_fontsize = max(20, base_size - max(0, (len(player) - threshold) * scaling_factor))
|
| 693 |
+
|
| 694 |
+
# Add the player name
|
| 695 |
+
ax.text(0.27, 0.5, f'{player}', transform=ax.transAxes, ha='left', va='center',
|
| 696 |
+
fontsize=adjusted_fontsize, style='italic')
|
| 697 |
+
|
| 698 |
+
# Add the metric value
|
| 699 |
+
ax.text(0.90, 0.5, f'{team_row["total"]:{format_string}}', transform=ax.transAxes,
|
| 700 |
+
ha='center', va='center', fontsize=36, weight='bold')
|
| 701 |
+
|
| 702 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 703 |
# Adjust the spacing between subplots to place them on the borders
|
| 704 |
ax_top = fig.add_subplot(gs[0, :])
|
| 705 |
ax_bot = fig.add_subplot(gs[-1, :])
|
| 706 |
ax_left = fig.add_subplot(gs[:, 0])
|
| 707 |
ax_right = fig.add_subplot(gs[:, -1])
|
| 708 |
+
|
|
|
|
| 709 |
ax_top.axis('off')
|
| 710 |
ax_bot.axis('off')
|
| 711 |
ax_left.axis('off')
|
| 712 |
ax_right.axis('off')
|
| 713 |
+
|
| 714 |
ax_bot.text(s='By: @TJStats', x=0.1, y=0.5, fontsize=24, ha='left')
|
| 715 |
ax_bot.text(s='Data: MLB', x=0.9, y=0.5, fontsize=24, ha='right')
|
| 716 |
+
ax_bot.text(s=f"2025 MLB Season\n{datetime.datetime.today().strftime('%Y-%m-%d')}",
|
| 717 |
+
x=0.5, y=0.5, fontsize=16, ha='center')
|
| 718 |
+
|
|
|
|
| 719 |
ax_stat_select = fig.add_subplot(gs[1, 1])
|
| 720 |
ax_stat_select.set_xlim(0, 2.5)
|
| 721 |
+
|
|
|
|
|
|
|
| 722 |
blank_image = np.ones((100, 100, 3), dtype=np.uint8) * 255
|
| 723 |
+
ax_stat_select.imshow(blank_image, extent=[-0.76, -0.58, 0.05, 0.95], aspect=0.2)
|
| 724 |
+
|
| 725 |
+
ax_stat_select.text(0.90*2.5, 0.0, f'{stat_label}', ha='center', va='bottom',
|
| 726 |
+
fontsize=36, style='italic')
|
| 727 |
ax_stat_select.axis('off')
|
| 728 |
+
|
| 729 |
+
ax_top.text(s=f'{title_full}', x=0.5, y=0.0, fontsize=40, ha='center', va='bottom',
|
| 730 |
+
style='italic', weight='bold')
|
| 731 |
+
|
| 732 |
+
ax_watermark = fig.add_subplot(gs[1:-1, 1:-1], zorder=-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 733 |
ax_watermark.set_xticks([])
|
| 734 |
ax_watermark.set_yticks([])
|
| 735 |
+
ax_watermark.set_frame_on(False)
|
| 736 |
+
|
| 737 |
+
try:
|
| 738 |
+
img = Image.open('tj stats circle-01_new.jpg')
|
| 739 |
+
img = img.convert("LA")
|
| 740 |
+
ax_watermark.imshow(img, extent=[0, 1, 0, 1], origin='upper', zorder=-1, alpha=0.075)
|
| 741 |
+
except Exception as e:
|
| 742 |
+
print(f"Failed to load watermark image: {e}")
|
| 743 |
+
|
| 744 |
@output
|
| 745 |
@render.plot
|
| 746 |
@reactive.event(input.generate_plot, ignore_none=True)
|