streambricks 0.2.17__tar.gz → 0.2.19__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. {streambricks-0.2.17 → streambricks-0.2.19}/PKG-INFO +1 -1
  2. streambricks-0.2.19/docs/profile_widget.md +89 -0
  3. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/__init__.py +6 -1
  4. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/auth/__init__.py +2 -0
  5. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/auth/helpers.py +7 -6
  6. streambricks-0.2.19/src/streambricks/auth/profile_widget.py +196 -0
  7. streambricks-0.2.19/src/streambricks/sidebar.py +30 -0
  8. {streambricks-0.2.17 → streambricks-0.2.19}/.copier-answers.yml +0 -0
  9. {streambricks-0.2.17 → streambricks-0.2.19}/.github/FUNDING.yml +0 -0
  10. {streambricks-0.2.17 → streambricks-0.2.19}/.github/copilot-instructions.md +0 -0
  11. {streambricks-0.2.17 → streambricks-0.2.19}/.github/dependabot.yml +0 -0
  12. {streambricks-0.2.17 → streambricks-0.2.19}/.github/workflows/build.yml +0 -0
  13. {streambricks-0.2.17 → streambricks-0.2.19}/.github/workflows/documentation.yml +0 -0
  14. {streambricks-0.2.17 → streambricks-0.2.19}/.gitignore +0 -0
  15. {streambricks-0.2.17 → streambricks-0.2.19}/.pre-commit-config.yaml +0 -0
  16. {streambricks-0.2.17 → streambricks-0.2.19}/LICENSE +0 -0
  17. {streambricks-0.2.17 → streambricks-0.2.19}/README.md +0 -0
  18. {streambricks-0.2.17 → streambricks-0.2.19}/docs/.empty +0 -0
  19. {streambricks-0.2.17 → streambricks-0.2.19}/duties.py +0 -0
  20. {streambricks-0.2.17 → streambricks-0.2.19}/mkdocs.yml +0 -0
  21. {streambricks-0.2.17 → streambricks-0.2.19}/overrides/_dummy.txt +0 -0
  22. {streambricks-0.2.17 → streambricks-0.2.19}/pyproject.toml +0 -0
  23. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/auth/decorator.py +0 -0
  24. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/auth/models.py +0 -0
  25. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/helpers.py +0 -0
  26. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/py.typed +0 -0
  27. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/state.py +0 -0
  28. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/widgets/__init__.py +0 -0
  29. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/widgets/bind_kwargs.py +0 -0
  30. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/widgets/image_capture.py +0 -0
  31. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/widgets/model_selector.py +0 -0
  32. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/widgets/model_widget/__init__.py +0 -0
  33. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/widgets/model_widget/field_metadata_renderers.py +0 -0
  34. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/widgets/model_widget/main.py +0 -0
  35. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/widgets/model_widget/type_renderers.py +0 -0
  36. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/widgets/multi_select.py +0 -0
  37. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/widgets/sequence.py +0 -0
  38. {streambricks-0.2.17 → streambricks-0.2.19}/src/streambricks/widgets/type_helpers.py +0 -0
  39. {streambricks-0.2.17 → streambricks-0.2.19}/tests/__init__.py +0 -0
  40. {streambricks-0.2.17 → streambricks-0.2.19}/tests/conftest.py +0 -0
  41. {streambricks-0.2.17 → streambricks-0.2.19}/tests/test_widgets.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: streambricks
3
- Version: 0.2.17
3
+ Version: 0.2.19
4
4
  Summary: Streamlit widgets and helpers
5
5
  Project-URL: Documentation, https://phil65.github.io/streambricks/
6
6
  Project-URL: Source, https://github.com/phil65/streambricks
@@ -0,0 +1,89 @@
1
+ # Profile Widget
2
+
3
+ The profile widget provides a simple way to display user profile information in Streamlit apps. It shows a profile picture (if available) or generates an avatar with user initials and displays the user's name.
4
+
5
+ ## Features
6
+
7
+ - Uses Google profile picture if available
8
+ - Generates a colorful avatar with user initials if no picture is available
9
+ - Supports different sizes: small, medium, large
10
+ - Optional name display
11
+ - Custom name override option
12
+
13
+ ## Usage
14
+
15
+ ```python
16
+ import streamlit as st
17
+ from streambricks import profile_widget, google_login, microsoft_login
18
+
19
+ # Login with your preferred method
20
+ user = google_login("Login to see profile")
21
+ # OR
22
+ # user = microsoft_login("Login with Microsoft")
23
+
24
+ if user:
25
+ # Basic usage
26
+ profile_widget(user)
27
+
28
+ # Customize size
29
+ profile_widget(user, size="medium") # Options: "small", "medium", "large"
30
+
31
+ # Without name
32
+ profile_widget(user, show_name=False)
33
+
34
+ # Custom display name
35
+ profile_widget(user, display_name="Custom Name")
36
+ ```
37
+
38
+ ## API Reference
39
+
40
+ ```python
41
+ def profile_widget(
42
+ user: GoogleUser | MicrosoftUser | None = None,
43
+ display_name: str | None = None,
44
+ size: Literal["small", "medium", "large"] = "small",
45
+ show_name: bool = True,
46
+ key: str | None = None,
47
+ ) -> None:
48
+ ```
49
+
50
+ ### Parameters
51
+
52
+ - **user**: A `GoogleUser` or `MicrosoftUser` object obtained from login methods
53
+ - **display_name**: Optional custom name to display instead of the user's name
54
+ - **size**: Size of the avatar ("small", "medium", or "large")
55
+ - **show_name**: Whether to show the name beside the avatar
56
+ - **key**: Optional unique key for the Streamlit widget
57
+
58
+ ## Examples
59
+
60
+ ### Different Sizes
61
+
62
+ ```python
63
+ st.write("Available sizes:")
64
+ col1, col2, col3 = st.columns(3)
65
+
66
+ with col1:
67
+ st.caption("Small (default)")
68
+ profile_widget(user, size="small")
69
+
70
+ with col2:
71
+ st.caption("Medium")
72
+ profile_widget(user, size="medium")
73
+
74
+ with col3:
75
+ st.caption("Large")
76
+ profile_widget(user, size="large")
77
+ ```
78
+
79
+ ### Usage in Header
80
+
81
+ ```python
82
+ col1, col2 = st.columns([1, 5])
83
+
84
+ with col1:
85
+ profile_widget(user, show_name=False, size="medium")
86
+
87
+ with col2:
88
+ st.title("Dashboard")
89
+ ```
@@ -1,6 +1,6 @@
1
1
  """StreamBricks components for Streamlit."""
2
2
 
3
- __version__ = "0.2.17"
3
+ __version__ = "0.2.19"
4
4
 
5
5
 
6
6
  from streambricks.auth import (
@@ -9,6 +9,7 @@ from streambricks.auth import (
9
9
  get_current_user,
10
10
  google_login,
11
11
  microsoft_login,
12
+ profile_widget,
12
13
  requires_login,
13
14
  )
14
15
  from streambricks.widgets.model_widget import (
@@ -24,6 +25,7 @@ from streambricks.widgets.model_selector import (
24
25
  from streambricks.helpers import run
25
26
  from streambricks.state import State
26
27
  from streambricks.widgets.bind_kwargs import bind_kwargs_as_widget
28
+ from streambricks.sidebar import hide_sidebar, set_sidebar_width
27
29
 
28
30
  __all__ = [
29
31
  "GoogleUser",
@@ -33,6 +35,7 @@ __all__ = [
33
35
  "bind_kwargs_as_widget",
34
36
  "get_current_user",
35
37
  "google_login",
38
+ "hide_sidebar",
36
39
  "image_capture",
37
40
  "llm_model_selector",
38
41
  "microsoft_login",
@@ -40,6 +43,8 @@ __all__ = [
40
43
  "model_edit",
41
44
  "model_selector",
42
45
  "multiselect",
46
+ "profile_widget",
43
47
  "requires_login",
44
48
  "run",
49
+ "set_sidebar_width",
45
50
  ]
@@ -3,6 +3,7 @@ from __future__ import annotations
3
3
  from streambricks.auth.decorator import requires_login
4
4
  from streambricks.auth.helpers import get_current_user, google_login, microsoft_login
5
5
  from streambricks.auth.models import GoogleUser, MicrosoftUser
6
+ from streambricks.auth.profile_widget import profile_widget
6
7
 
7
8
  __all__ = [
8
9
  "GoogleUser",
@@ -10,5 +11,6 @@ __all__ = [
10
11
  "get_current_user",
11
12
  "google_login",
12
13
  "microsoft_login",
14
+ "profile_widget",
13
15
  "requires_login",
14
16
  ]
@@ -17,7 +17,8 @@ def _get_user_data() -> dict[str, Any] | None:
17
17
  return {**st.user} # pyright: ignore
18
18
 
19
19
  # Fall back to experimental version
20
- if hasattr(st, "experimental_user") and st.experimental_user.is_logged_in:
20
+ # if hasattr(st, "experimental_user") and getattr(st.experimental_user, "is_logged_in", False): # noqa: E501
21
+ if st.experimental_user and st.experimental_user.get("is_logged_in"):
21
22
  return {**st.experimental_user}
22
23
 
23
24
  return None
@@ -41,7 +42,7 @@ def google_login(
41
42
  user_data = _get_user_data()
42
43
  if user_data is not None:
43
44
  # User is already logged in, return typed user object
44
- return GoogleUser(is_logged_in=True, **user_data) # pyright: ignore
45
+ return GoogleUser(**user_data) # pyright: ignore
45
46
 
46
47
  # User not logged in, display login button
47
48
  if st.button(button_text, key=key):
@@ -69,7 +70,7 @@ def microsoft_login(
69
70
  user_data = _get_user_data()
70
71
  if user_data is not None:
71
72
  # User is already logged in, return typed user object
72
- return MicrosoftUser(is_logged_in=True, **user_data) # pyright: ignore
73
+ return MicrosoftUser(**user_data) # pyright: ignore
73
74
 
74
75
  # User not logged in, display login button
75
76
  if st.button(button_text, key=key):
@@ -112,11 +113,11 @@ def get_current_user(
112
113
  return None
113
114
 
114
115
  if user_class is not None:
115
- return user_class(is_logged_in=True, **user_data) # pyright: ignore
116
+ return user_class(**user_data) # pyright: ignore
116
117
  try:
117
- return GoogleUser(is_logged_in=True, **user_data) # pyright: ignore
118
+ return GoogleUser(**user_data) # pyright: ignore
118
119
  except Exception: # noqa: BLE001
119
120
  try:
120
- return MicrosoftUser(is_logged_in=True, **user_data) # pyright: ignore
121
+ return MicrosoftUser(**user_data) # pyright: ignore
121
122
  except Exception: # noqa: BLE001
122
123
  return None
@@ -0,0 +1,196 @@
1
+ from __future__ import annotations
2
+
3
+ import base64
4
+ import functools
5
+ import hashlib
6
+ from io import BytesIO
7
+ from typing import Literal
8
+
9
+ from PIL import Image, ImageDraw, ImageFont
10
+ import streamlit as st
11
+
12
+ from streambricks.auth.models import GoogleUser, MicrosoftUser
13
+
14
+
15
+ @st.cache_data(ttl=3600, show_spinner=False)
16
+ def _generate_avatar_image(
17
+ initials: str,
18
+ bg_color: str | tuple[int, int, int] = "#1f77b4",
19
+ text_color: str | tuple[int, int, int] = "white",
20
+ size: int = 100,
21
+ ) -> Image.Image:
22
+ """Generate an avatar image with user initials.
23
+
24
+ Args:
25
+ initials: The text to display on the avatar (usually initials)
26
+ bg_color: Background color as hex string or RGB tuple
27
+ text_color: Text color as hex string or RGB tuple
28
+ size: Size of the image in pixels
29
+
30
+ Returns:
31
+ PIL Image with generated avatar
32
+ """
33
+ # Create a square image with the specified background color
34
+ img = Image.new("RGB", (size, size), bg_color)
35
+ draw = ImageDraw.Draw(img)
36
+
37
+ # Try to load a font, fall back to default if not available
38
+ font_size = int(size * 0.4) # 40% of image size
39
+ try:
40
+ font: ImageFont.FreeTypeFont | ImageFont.ImageFont = ImageFont.truetype(
41
+ "arial.ttf", font_size
42
+ )
43
+ except OSError:
44
+ font = ImageFont.load_default()
45
+
46
+ text_width, text_height = draw.textbbox((0, 0), initials, font=font)[2:]
47
+ x = (size - text_width) // 2
48
+ y = (size - text_height) // 2
49
+ draw.text((x, y), initials, fill=text_color, font=font)
50
+ mask = Image.new("L", (size, size), 0)
51
+ mask_draw = ImageDraw.Draw(mask)
52
+ mask_draw.ellipse((0, 0, size, size), fill=255)
53
+ img.putalpha(mask)
54
+
55
+ return img
56
+
57
+
58
+ def _get_initials(name: str) -> str:
59
+ """Extract initials from a name.
60
+
61
+ Args:
62
+ name: Full name of the user
63
+
64
+ Returns:
65
+ Initials (up to 2 characters)
66
+ """
67
+ parts = name.strip().split()
68
+ if not parts:
69
+ return "?"
70
+
71
+ if len(parts) == 1:
72
+ return parts[0][0].upper()
73
+
74
+ return (parts[0][0] + parts[-1][0]).upper()
75
+
76
+
77
+ @functools.lru_cache(maxsize=128)
78
+ def _get_color_from_string(s: str) -> str:
79
+ """Generate a consistent color from a string.
80
+
81
+ Args:
82
+ s: Input string (like user ID)
83
+
84
+ Returns:
85
+ Hex color code
86
+ """
87
+ # Use a hash function to get a reproducible integer from the string
88
+ hash_obj = hashlib.md5(s.encode())
89
+ hash_int = int.from_bytes(hash_obj.digest()[:4], "little")
90
+
91
+ # Select from a set of good UI colors (excluding very light ones)
92
+ colors = [
93
+ "#1f77b4", # blue
94
+ "#ff7f0e", # orange
95
+ "#2ca02c", # green
96
+ "#d62728", # red
97
+ "#9467bd", # purple
98
+ "#8c564b", # brown
99
+ "#e377c2", # pink
100
+ "#7f7f7f", # gray
101
+ "#bcbd22", # olive
102
+ "#17becf", # teal
103
+ ]
104
+
105
+ return colors[hash_int % len(colors)]
106
+
107
+
108
+ @st.cache_data(ttl=3600, show_spinner=False)
109
+ def _image_to_base64(img_hash: int, img: Image.Image) -> str:
110
+ """Convert a PIL Image to a base64 string.
111
+
112
+ Args:
113
+ img_hash: Hash of the image parameters for caching
114
+ img: PIL Image object
115
+
116
+ Returns:
117
+ Base64 encoded string of the image
118
+ """
119
+ # The img_hash parameter is only used for caching and isn't used in the function
120
+ buffer = BytesIO()
121
+ img.save(buffer, format="PNG")
122
+ return base64.b64encode(buffer.getvalue()).decode("utf-8")
123
+
124
+
125
+ def profile_widget(
126
+ user: GoogleUser | MicrosoftUser | None = None,
127
+ display_name: str | None = None,
128
+ size: Literal["small", "medium", "large"] = "small",
129
+ show_name: bool = True,
130
+ key: str | None = None,
131
+ ) -> None:
132
+ """Display a profile widget with user avatar and name.
133
+
134
+ Args:
135
+ user: GoogleUser or MicrosoftUser object
136
+ display_name: Optional name override to display instead of user's name
137
+ size: Size of the avatar image ("small", "medium", or "large")
138
+ show_name: Whether to show the name next to the avatar
139
+ key: Optional key for the Streamlit widget
140
+
141
+ Returns:
142
+ None
143
+ """
144
+ if user is None:
145
+ return
146
+
147
+ size_map = {"small": 32, "medium": 48, "large": 64}
148
+ img_size = size_map[size]
149
+ name = display_name or user.name
150
+ css = f"""
151
+ <style>
152
+ .profile-widget-{key if key else "default"} {{
153
+ display: flex;
154
+ align-items: center;
155
+ margin: 5px 0;
156
+ }}
157
+ .avatar-{key if key else "default"} {{
158
+ border-radius: 50%;
159
+ width: {img_size}px;
160
+ height: {img_size}px;
161
+ object-fit: cover;
162
+ }}
163
+ .profile-name-{key if key else "default"} {{
164
+ margin-left: 10px;
165
+ font-size: {int(img_size * 0.4)}px;
166
+ }}
167
+ </style>
168
+ """
169
+
170
+ img_src = ""
171
+ if isinstance(user, GoogleUser) and user.picture:
172
+ img_src = user.picture
173
+ else:
174
+ # Generate avatar with initials
175
+ initials = _get_initials(name)
176
+ color = _get_color_from_string(user.user_id)
177
+ # Double size for better quality
178
+ avatar_img = _generate_avatar_image(initials, color, size=img_size * 2)
179
+ # Create a hash of the parameters for caching the base64 conversion
180
+ img_hash = hash((initials, color, img_size))
181
+ img_src = f"data:image/png;base64,{_image_to_base64(img_hash, avatar_img)}"
182
+
183
+ # HTML for the widget
184
+ html = f"""
185
+ {css}
186
+ <div class="profile-widget-{key if key else "default"}">
187
+ <img src="{img_src}" class="avatar-{key if key else "default"}" alt="User Avatar">
188
+ {
189
+ f'<span class="profile-name-{key if key else "default"}">{name}</span>'
190
+ if show_name
191
+ else ""
192
+ }
193
+ </div>
194
+ """
195
+
196
+ st.markdown(html, unsafe_allow_html=True)
@@ -0,0 +1,30 @@
1
+ import streamlit as st
2
+
3
+
4
+ HIDE_SIDEBAR = """\
5
+ <style>
6
+ [data-testid="stSidebar"][aria-expanded="true"] {display: none;}
7
+ [data-testid="stSidebar"][aria-expanded="false"] {display: none;}
8
+ </style>
9
+ """
10
+
11
+
12
+ def set_sidebar_width(width: int):
13
+ st.markdown(
14
+ f"""\
15
+ <style>
16
+ [data-testid="stSidebar"][aria-expanded="true"] > div:first-child {{
17
+ width: {width}px;
18
+ }}
19
+ [data-testid="stSidebar"][aria-expanded="false"] > div:first-child {{
20
+ width: {width}px;
21
+ margin-left: -{width}px;
22
+ }}
23
+ </style>
24
+ """,
25
+ unsafe_allow_html=True,
26
+ )
27
+
28
+
29
+ def hide_sidebar():
30
+ st.markdown(HIDE_SIDEBAR, unsafe_allow_html=True)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes