import gradio as gr import html def create_player_card_component(player_data=None): """ Creates a Gradio HTML component to display player information based on test.ipynb structure. Args: player_data (dict, optional): Dictionary containing player data. Expected keys: 'Name', 'Position', 'Jersey_number', 'headshot_url', 'College', 'Height', 'Weight', 'Years_in_nfl', 'instagram_url'. Defaults to None. Returns: gr.HTML: A Gradio HTML component displaying the player card, or empty if no data. """ print("--- Entered create_player_card_component ---") # DEBUG LOG if not player_data or not isinstance(player_data, dict): print("Component received no player data, returning empty.") # DEBUG LOG return gr.HTML("") print(f"Component received player_data: {player_data}") # DEBUG LOG try: # Extract data with defaults, using html.escape name = html.escape(player_data.get('Name', 'N/A')) position = html.escape(player_data.get('Position', '')) number = html.escape(str(player_data.get('Jersey_number', ''))) headshot_url = html.escape(player_data.get('headshot_url', '')) college = html.escape(player_data.get('College', 'N/A')) height = html.escape(player_data.get('Height', 'N/A')) weight = html.escape(player_data.get('Weight', 'N/A')) exp = html.escape(str(player_data.get('Years_in_nfl', 'N/A'))) instagram_url = html.escape(player_data.get('instagram_url', '')) # CSS from test.ipynb, adapted slightly for theme integration css = """ """ # HTML structure based on test.ipynb # Using an outer container for better layout control in Gradio html_content = f""" {css}
{name} Headshot

{name} {f'- #{number}' if number else ''} {f'({position})' if position else ''}

  • Ht: {height} | Wt: {weight} lbs
  • College: {college}
  • Experience: {exp} Years
""" # Add Instagram link conditionally if instagram_url: html_content += f""" """ html_content += """
""" print(f"Component generated HTML (first 100 chars): {html_content[:100]}...") # DEBUG LOG return gr.HTML(html_content) except Exception as e: print(f"Error creating player card component: {str(e)}") # Return a simple error message component return gr.HTML("
⚠️ Error loading player card.
") # Example Usage (for testing component independently if needed) # if __name__ == '__main__': # example_data = { # 'Name': 'Brock Purdy', # 'headshot_url': 'https://a.espncdn.com/i/headshots/nfl/players/full/4433216.png', # Example URL # 'instagram_url': 'https://www.instagram.com/brock.purdy13/', # 'Position': 'QB', # 'Number': '13' # } # component = create_player_card_component(example_data) # # with gr.Blocks() as demo: # gr.Markdown("## Player Card Example") # demo.add(component) # # demo.launch()