File size: 855 Bytes
aa893a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import io
import base64
import matplotlib.pyplot as plt


def figure_to_png_bytes(fig):
    """
    Convert a matplotlib figure to PNG bytes.
    Useful for exporting images in Gradio.
    """
    buf = io.BytesIO()
    fig.savefig(buf, format="png", dpi=150, bbox_inches="tight")
    buf.seek(0)
    return buf.getvalue()


def safe_get(d, key, default=None):
    """
    Helper to safely get values from dictionaries.
    """
    if d is None:
        return default
    return d.get(key, default)


def dict_to_text(d):
    """
    Simple helper to display a dictionary as multi-line text.
    Useful for showing the trend summary in the Insights tab.
    """
    if not isinstance(d, dict) or len(d) == 0:
        return "No insights available."

    lines = []
    for k, v in d.items():
        lines.append(f"{k}: {v}")
    return "\n".join(lines)