Repository navigation
Expand file tree
/
Copy pathexample.py
More file actions
368 lines (334 loc) · 13.9 KB
/
Copy pathexample.py
File metadata and controls
368 lines (334 loc) · 13.9 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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
#!/usr/bin/env python3
import streamlit as st
import pandas as pd
import os
from streamlit_swipecards import streamlit_swipecards
st.set_page_config(page_title="Streamlit Swipe Cards Example")
st.write("# 📱 Streamlit Swipe Cards Demo")
st.markdown("""
This component brings swipe functionality to your Streamlit app!
Users can swipe through cards containing images or data with intuitive gestures.
## How to use:
1. **Install the component**: `pip install streamlit-swipecards`
2. **Import it**: `from streamlit_swipecards import streamlit_swipecards`
3. **Use it**: Create cards with images or data tables and let users swipe through them
""")
st.write("## 📱 Interactive Examples")
# Border toggle and mode selection
st.write("Choose customization options:")
show_border = st.checkbox("Show card borders", True)
view_option = st.radio(
"Select view:",
["Mobile", "Tablet", "Desktop"],
horizontal=True
)
view_mapping = {"Mobile": "mobile", "Tablet": "tablet", "Desktop": "desktop"}
view = view_mapping[view_option]
mode = st.radio(
"Select display mode:",
["Image Cards", "Table Cards"],
horizontal=True
)
# Table display controls
st.markdown("### Table display options:")
col_a, col_b, col_c = st.columns(3)
with col_a:
table_font_size = st.slider("Font size (px)", 12, 20, 14)
with col_b:
table_max_rows = st.number_input("Max rows (optional)", min_value=0, value=0, step=1, help="0 = show all")
with col_c:
table_max_columns = st.number_input("Max columns (optional)", min_value=0, value=0, step=1, help="0 = show all")
if mode == "Image Cards":
st.subheader("🖼️ Image Cards Mode")
# Sample cards data
sample_cards = [
{
"name": "Alice",
"description": "Loves hiking and photography",
"image": "https://images.unsplash.com/photo-1544005313-94ddf0286df2?w=400&h=400&fit=crop&crop=faces",
"pills": ["Pill 1", "Pill 2", "Pill 3", "Pill 4"]
},
{
"name": "Bob",
"description": "Chef and food enthusiast",
"image": "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=400&h=400&fit=crop&crop=faces",
"pills": ["Pill 1", "Pill 2", "Pill 3", "Pill 4"]
},
{
"name": "Carol",
"description": "Artist and musician",
"image": "https://images.unsplash.com/photo-1438761681033-6461ffad8d80?w=400&h=400&fit=crop&crop=faces",
"pills": ["Pill 1", "Pill 2", "Pill 3", "Pill 4"]
}
]
st.markdown("### Instructions:")
st.markdown("- 👆 **Swipe right** or click 💚 to like")
st.markdown("- 👆 **Swipe left** or click ❌ to pass")
st.markdown("- 🔄 Click ↶ to go back")
# Create the swipe cards
result = streamlit_swipecards(
cards=sample_cards,
display_mode="cards",
show_border=show_border,
view=view,
last_card_message="This is the last page. You can add your own text here",
key="image_example",
)
if result:
st.session_state["swipe_results"] = result
else: # Table Cards
st.subheader("📊 Table Cards Mode")
# Load sample data from CSV file
csv_path = os.path.join(os.path.dirname(__file__), "sample_data.csv")
# Check if CSV file exists, if not create it
if not os.path.exists(csv_path):
st.error(f"Sample data file not found at {csv_path}")
st.write("Please make sure sample_data.csv exists in the project directory.")
st.stop()
# Load and display the dataset
try:
df = pd.read_csv(csv_path)
st.write("**Head of Sample Dataset:**")
st.dataframe(df.head()) # Show only first 5 rows in preview
except Exception as e:
st.error(f"Error loading sample data: {str(e)}")
st.stop()
st.markdown("### Instructions:")
st.markdown("- 👆 **Swipe right** or click 💚 to like the row")
st.markdown("- 👆 **Swipe left** or click ❌ to pass the row")
st.markdown("- 🔄 Click ↶ to go back")
# Create table cards - each card represents a different row with its own configuration
table_cards = [
{
"dataset_path": csv_path,
"row_index": 0, # Alice Johnson
"name": "Alice Johnson",
"description": "Engineering professional from New York",
"pills": ["Pill 1", "Pill 2", "Pill 3", "Pill 4"],
# Demonstrates default highlighting colors for cell, row and column
"highlight_cells": [{'row': 0, 'column': 'Salary'}],
"highlight_rows": [{"row": 0}],
"highlight_columns": [{"column": "Salary"}],
"center_table_row": 0,
"center_table_column": "Salary"
},
{
"dataset_path": csv_path,
"row_index": 1, # Bob Smith
"name": "Bob Smith",
"description": "Sales professional from California",
"pills": ["Pill 1", "Pill 2", "Pill 3", "Pill 4"],
"highlight_cells": [{'row': 1, 'column': 'Rating', 'color': '#98FB98'}],
"highlight_rows": [{"row": 1}],
"highlight_columns": [{"column": "Rating"}],
"center_table_row": 1,
"center_table_column": "Rating"
},
{
"dataset_path": csv_path,
"row_index": 2, # Carol Davis
"name": "Carol Davis",
"description": "Marketing specialist from Texas",
"pills": ["Pill 1", "Pill 2", "Pill 3", "Pill 4"],
"highlight_cells": [{'row': 2, 'column': 'Experience', 'color': '#87CEEB'}],
"highlight_rows": [{"row": 2}],
"highlight_columns": [{"column": "Experience"}],
"center_table_row": 2,
"center_table_column": "Experience"
},
{
"dataset_path": csv_path,
"row_index": 3, # David Wilson
"name": "David Wilson",
"description": "Engineering manager from New York",
"pills": ["Pill 1", "Pill 2", "Pill 3", "Pill 4"],
"highlight_cells": [{'row': 3, 'column': 'Projects', 'color': '#DDA0DD'}],
"highlight_rows": [{"row": 3}],
"highlight_columns": [{"column": "Projects"}],
"center_table_row": 3,
"center_table_column": "Projects"
},
{
"dataset_path": csv_path,
"row_index": 4, # Eve Brown
"name": "Eve Brown",
"description": "HR specialist from Florida",
"pills": ["Pill 1", "Pill 2", "Pill 3", "Pill 4"],
"highlight_cells": [{'row': 4, 'column': 'Salary', 'color': '#F0E68C'}],
"highlight_rows": [{"row": 4}],
"highlight_columns": [{"column": "Salary"}],
"center_table_row": 4,
"center_table_column": "Salary"
},
{
"dataset_path": csv_path,
"row_index": 5,
"name": "Frank Miller",
"description": "Senior engineer from Seattle",
"pills": ["Pill 1", "Pill 2", "Pill 3", "Pill 4"],
"highlight_cells": [{"row": 5, "column": "Salary", "color": "#FFD700"}],
"highlight_rows": [{"row": 5}],
"highlight_columns": [{"column": "Salary"}],
"center_table_row": 5,
"center_table_column": "Salary"
},
{
"dataset_path": csv_path,
"row_index": 6,
"name": "Grace Lee",
"description": "UI/UX designer based in California",
"pills": ["Pill 1", "Pill 2", "Pill 3", "Pill 4"],
"highlight_cells": [{"row": 6, "column": "Skills", "color": "#87CEEB"}],
"highlight_rows": [{"row": 6}],
"highlight_columns": [{"column": "Skills"}],
"center_table_row": 6,
"center_table_column": "Skills"
},
{
"dataset_path": csv_path,
"row_index": 8,
"name": "Iris Chen",
"description": "Data scientist specializing in ML & SQL",
"pills": ["Pill 1", "Pill 2", "Pill 3", "Pill 4"],
"highlight_cells": [{"row": 8, "column": "Rating", "color": "#98FB98"}],
"highlight_rows": [{"row": 8}],
"highlight_columns": [{"column": "Rating"}],
"center_table_row": 8,
"center_table_column": "Rating"
},
{
"dataset_path": csv_path,
"row_index": 9,
"name": "Jack Wilson",
"description": "Marketing associate from Texas",
"pills": ["Pill 1", "Pill 2", "Pill 3", "Pill 4"],
"highlight_cells": [{"row": 9, "column": "Experience", "color": "#FFA500"}],
"highlight_rows": [{"row": 9}],
"highlight_columns": [{"column": "Experience"}],
"center_table_row": 9,
"center_table_column": "Experience"
},
{
"dataset_path": csv_path,
"row_index": 10,
"name": "Henry Taylor",
"description": "Financial analyst from New York",
"pills": ["Pill 1", "Pill 2", "Pill 3", "Pill 4"],
"highlight_cells": [{"row": 10, "column": "Location", "color": "#E6E6FA"}],
"highlight_rows": [{"row": 10}],
"highlight_columns": [{"column": "Location"}],
"center_table_row": 10,
"center_table_column": "Location"
}
]
# Create the table swipe cards
result = streamlit_swipecards(
cards=table_cards,
display_mode="table",
show_border=show_border,
table_font_size=table_font_size,
table_max_rows=int(table_max_rows) if table_max_rows else None,
table_max_columns=int(table_max_columns) if table_max_columns else None,
view=view,
center_table_row=0,
center_table_column="Salary",
last_card_message="This is the last page. You can add your own text here",
key="table_example"
)
if result:
st.session_state["swipe_results"] = result
if st.session_state.get("swipe_results"):
st.write("### Results:")
st.json(st.session_state["swipe_results"])
st.write("## 🛠️ Code Examples")
st.write("💡 **Tip**: You can create both image cards and table cards with rich customization options!")
st.markdown("""
### Image Cards Example
```python
from streamlit_swipecards import streamlit_swipecards
# Create image cards
sample_cards = [
{
"name": "Alice",
"description": "Loves hiking and photography",
"image": "https://images.unsplash.com/photo-1544005313-94ddf0286df2?w=400&h=400&fit=crop&crop=faces",
"pills": ["Adventure", "Photography", "Nature"]
},
{
"name": "Bob",
"description": "Chef and food enthusiast",
"image": "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=400&h=400&fit=crop&crop=faces",
"pills": ["Cooking", "Food", "Travel"]
}
]
result = streamlit_swipecards(
cards=sample_cards,
display_mode="cards",
key="image_swiper"
)
```
### Table Cards Example
```python
from streamlit_swipecards import streamlit_swipecards
# Create table cards with highlighting
table_cards = [
{
"dataset_path": "data.csv",
"row_index": 0,
"name": "Alice Johnson",
"description": "Engineering professional from New York",
"pills": ["Engineer", "NYC", "Senior"],
"highlight_cells": [{'row': 0, 'column': 'Salary'}],
"highlight_rows": [{"row": 0}],
"highlight_columns": [{"column": "Salary"}],
"center_table_row": 0,
"center_table_column": "Salary"
}
]
result = streamlit_swipecards(
cards=table_cards,
display_mode="table",
key="table_swiper"
)
```
""")
st.write("## ⚙️ Parameters")
st.markdown("""
### streamlit_swipecards()
| Parameter | Type | Description | Required |
|-----------|------|-------------|----------|
| `cards` | list[dict] | List of card objects to display | Yes |
| `display_mode` | str | Display mode: "cards" for images or "table" for data tables | Yes |
| `view` | str | Layout size: "mobile" (360px), "tablet" (800px), or "desktop" (full width) | No |
| `show_border` | bool | Whether to display a border around cards | No |
| `table_font_size` | int | Table font size (px) in table mode | No |
| `table_max_rows` | int | Max rows to render per table card | No |
| `table_max_columns` | int | Max columns to render per table card | No |
| `last_card_message` | str | Custom text shown when all cards are swiped | No |
| `key` | str | Unique component key for state management | Yes |
### Card Object (Image Cards)
| Parameter | Type | Description | Required |
|-----------|------|-------------|----------|
| `name` | str | Name/title displayed on the card | Yes |
| `description` | str | Description text shown on the card | Yes |
| `image` | str | URL to the image to display | Yes |
| `pills` | list[str] | List of pill/tag labels to show | No |
### Card Object (Table Cards)
| Parameter | Type | Description | Required |
|-----------|------|-------------|----------|
| `dataset_path` | str | Path to the CSV file containing data | Yes |
| `row_index` | int | Index of the row to highlight/center on | Yes |
| `name` | str | Name/title for the card | Yes |
| `description` | str | Description text for the card | Yes |
| `pills` | list[str] | List of pill/tag labels to show | No |
| `highlight_cells` | list[dict] | Cells to highlight: `[{'row': 0, 'column': 'Name', 'color': '#FF0000'}]` | No |
| `highlight_rows` | list[dict] | Rows to highlight: `[{'row': 0, 'color': '#00FF00'}]` | No |
| `highlight_columns` | list[dict] | Columns to highlight: `[{'column': 'Salary', 'color': '#0000FF'}]` | No |
| `center_table_row` | int | Row index to center the table view on | No |
| `center_table_column` | str | Column name to center the table view on | No |
**Returns:** `dict` - Result object containing swipe data and statistics
- `totalSwiped`: Total number of cards swiped
- `liked`: List of liked card indices
- `passed`: List of passed card indices
- `currentIndex`: Current card index being displayed
""")