๐คย ย Avatar
Submitted by Lukas Masuch
Summary
Display a circular avatar image with optional label and caption.
Functions
avatar
Display a circular avatar image with optional label and caption.
This component renders a circular avatar image, commonly used for user profiles, contact lists, or any UI that needs to display a person or entity visually. Optionally displays a label and caption to the right of the image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
ImageLike
|
The image to display. Supports URLs (str), file paths (str or Path), PIL images, numpy arrays, or raw bytes/BytesIO. |
required |
label
|
str | None
|
Optional primary text displayed to the right of the avatar. Typically used for names or titles. |
None
|
caption
|
str | None
|
Optional secondary text displayed below the label. Typically used for roles, status, or descriptions. |
None
|
height
|
int
|
Avatar height (and width) in pixels. Defaults to 48. |
48
|
width
|
Literal['stretch', 'content'] | int
|
Component width. "content" (default) sizes to fit the content. "stretch" fills the container width. An integer sets a fixed pixel width. |
'content'
|
border
|
bool
|
If True, adds a subtle border around the avatar image. Defaults to False. |
False
|
on_click
|
Literal['ignore', 'rerun'] | Callable[[], None]
|
Click behavior. "ignore" (default) disables click interactions. "rerun" triggers an app rerun when clicked. Pass a callable to execute a custom callback when clicked. |
'ignore'
|
key
|
str | None
|
Unique key for this component instance. Required when using multiple avatars with the same parameters. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the avatar was clicked on this run (when on_click != "ignore"), |
bool
|
False otherwise. Always returns False when on_click is "ignore". |
Raises:
| Type | Description |
|---|---|
StreamlitAPIException
|
If height is less than 1 or on_click has an invalid value. |
Example
Simple avatar:
Avatar with label and caption:
avatar(
"https://avatars.githubusercontent.com/u/12345",
label="John Doe",
caption="Software Engineer",
)
Clickable avatar:
Source code in src/streamlit_extras/avatar/__init__.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | |
Import:
- You should add this to the top of your .py file
Examples
example
def example() -> None:
"""Example usage of the avatar component."""
st.subheader("Basic avatars")
col1, col2, col3 = st.columns(3)
with col1:
avatar(
"https://avatars.githubusercontent.com/u/1673013?v=4",
label="Adrien Treuille",
caption="Co-founder",
)
with col2:
avatar(
"https://avatars.githubusercontent.com/u/690814?v=4",
label="Thiago Teixeira",
caption="Co-founder",
)
with col3:
avatar(
"https://avatars.githubusercontent.com/u/47222480?v=4",
label="Amanda Kelly",
caption="Co-founder",
)
st.subheader("Different heights")
col1, col2, col3 = st.columns(3)
with col1:
avatar(
"https://avatars.githubusercontent.com/u/1673013?v=4",
height=32,
label="Small (32px)",
)
with col2:
avatar(
"https://avatars.githubusercontent.com/u/1673013?v=4",
height=48,
label="Medium (48px)",
)
with col3:
avatar(
"https://avatars.githubusercontent.com/u/1673013?v=4",
height=64,
label="Large (64px)",
)
st.subheader("Clickable avatar")
if avatar(
"https://avatars.githubusercontent.com/u/1673013?v=4",
label="Click me!",
caption="I'm interactive",
on_click="rerun",
key="clickable_avatar",
):
st.toast("Avatar clicked!")
st.subheader("Avatar only (no text)")
with st.container(horizontal=True):
avatar("https://avatars.githubusercontent.com/u/1673013?v=4", height=40)
avatar("https://avatars.githubusercontent.com/u/690814?v=4", height=40)
avatar("https://avatars.githubusercontent.com/u/47222480?v=4", height=40)
avatar("https://avatars.githubusercontent.com/u/12345", height=40)
st.subheader("Avatars with border")
with st.container(horizontal=True):
avatar("https://avatars.githubusercontent.com/u/1673013?v=4", height=40, border=True)
avatar("https://avatars.githubusercontent.com/u/690814?v=4", height=40, border=True)
avatar("https://avatars.githubusercontent.com/u/47222480?v=4", height=40, border=True)
avatar("https://avatars.githubusercontent.com/u/12345", height=40, border=True)