Last active
July 25, 2025 23:03
-
-
Save kazuar/a33a82e702354f5da0f89bcd848632b9 to your computer and use it in GitHub Desktop.
streamlit-json-tip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import streamlit as st | |
| from streamlit_json_tip import json_viewer | |
| # Sample JSON data | |
| dynamic_data = [ | |
| {"name": "john", "spirit_animal": "dog", "age": 25}, | |
| {"name": "jake", "spirit_animal": "cow", "age": 30}, | |
| {"name": "alice", "spirit_animal": "cat", "age": 28} | |
| ] | |
| # Dynamic tooltip function | |
| def create_dynamic_tooltip(path, value, full_data): | |
| """Create custom tooltips based on field path, value, and context""" | |
| # Score tooltips for names based on length | |
| if path.endswith(".name") and isinstance(value, str): | |
| score = len(value) * 2 # Simple scoring: 2 points per character | |
| return f"Name score: {score} points" | |
| # Age category tooltips | |
| if path.endswith(".age") and isinstance(value, int): | |
| if value < 25: | |
| return "Category: Young Adult" | |
| elif value < 30: | |
| return "Category: Adult" | |
| else: | |
| return "Category: Mature Adult" | |
| # Spirit animal rarity tooltips | |
| if path.endswith(".spirit_animal") and isinstance(value, str): | |
| rarity_map = { | |
| "dog": "Common (found in 60% of profiles)", | |
| "cat": "Uncommon (found in 25% of profiles)", | |
| "cow": "Rare (found in 5% of profiles)", | |
| "dragon": "Legendary (found in 0.1% of profiles)" | |
| } | |
| return rarity_map.get(value, "Unknown rarity") | |
| # Array element tooltips | |
| if path.startswith("[") and "].name" in path: | |
| # Extract index from path like "[0].name" | |
| try: | |
| index = int(path.split("]")[0][1:]) | |
| return f"Person #{index + 1} in the list" | |
| except: | |
| pass | |
| return None | |
| st.title("Hello from streamlit-tests!") | |
| selected_dynamic = json_viewer( | |
| data=dynamic_data, | |
| dynamic_tooltips=create_dynamic_tooltip, | |
| height=300, | |
| key="dynamic_tooltip_demo" | |
| ) | |
| if selected_dynamic: | |
| st.write(f"**Selected:** {selected_dynamic.get('path')} = {selected_dynamic.get('value')}") | |
| if selected_dynamic.get('help_text'): | |
| st.write(f"**Dynamic Tooltip:** {selected_dynamic.get('help_text')}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
