streamlit-react-components 1.0.6__py3-none-any.whl → 1.0.7__py3-none-any.whl

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.
@@ -131,12 +131,230 @@ def _dataframe_to_waterfall(
131
131
  }
132
132
 
133
133
 
134
+ def _dataframe_to_scatter(
135
+ data: Any,
136
+ x: str,
137
+ y: str,
138
+ size: Optional[str] = None,
139
+ color_column: Optional[str] = None,
140
+ text: Optional[str] = None,
141
+ title: Optional[str] = None,
142
+ ) -> Dict[str, Any]:
143
+ """Convert DataFrame to Plotly scatter figure."""
144
+ from ..themes import get_active_theme
145
+
146
+ # Validate required columns
147
+ if x not in data.columns:
148
+ raise ValueError(f"Column '{x}' not found in DataFrame. Available columns: {list(data.columns)}")
149
+ if y not in data.columns:
150
+ raise ValueError(f"Column '{y}' not found in DataFrame. Available columns: {list(data.columns)}")
151
+
152
+ # Build marker config
153
+ marker_config = {}
154
+ if size and size in data.columns:
155
+ marker_config['size'] = data[size].tolist()
156
+ if color_column and color_column in data.columns:
157
+ marker_config['color'] = data[color_column].tolist()
158
+ marker_config['colorscale'] = 'Viridis'
159
+ marker_config['showscale'] = True
160
+
161
+ # Build hover text
162
+ hover_text = data[text].tolist() if text and text in data.columns else None
163
+
164
+ return {
165
+ 'data': [{
166
+ 'type': 'scatter',
167
+ 'mode': 'markers',
168
+ 'x': data[x].tolist(),
169
+ 'y': data[y].tolist(),
170
+ 'marker': marker_config,
171
+ 'text': hover_text,
172
+ 'hovertemplate': f'{x}: %{{x}}<br>{y}: %{{y}}<extra></extra>'
173
+ }],
174
+ 'layout': {
175
+ 'title': title or '',
176
+ 'xaxis': {'title': x},
177
+ 'yaxis': {'title': y}
178
+ }
179
+ }
180
+
181
+
182
+ def _dataframe_to_bar(
183
+ data: Any,
184
+ x: str,
185
+ y: Union[str, List[str]],
186
+ orientation: str = 'v',
187
+ barmode: str = 'group',
188
+ title: Optional[str] = None,
189
+ ) -> Dict[str, Any]:
190
+ """Convert DataFrame to Plotly bar figure."""
191
+ from ..themes import get_active_theme
192
+
193
+ theme = get_active_theme()
194
+ colors = [
195
+ theme['colors']['primary'],
196
+ theme['colors']['secondary'],
197
+ theme['colors']['success'],
198
+ theme['colors']['warning'],
199
+ theme['colors']['info']
200
+ ]
201
+
202
+ # Validate columns
203
+ if x not in data.columns:
204
+ raise ValueError(f"Column '{x}' not found in DataFrame. Available columns: {list(data.columns)}")
205
+
206
+ y_cols = [y] if isinstance(y, str) else y
207
+ for y_col in y_cols:
208
+ if y_col not in data.columns:
209
+ raise ValueError(f"Column '{y_col}' not found in DataFrame. Available columns: {list(data.columns)}")
210
+
211
+ traces = []
212
+ for idx, y_col in enumerate(y_cols):
213
+ if orientation == 'v':
214
+ trace = {
215
+ 'type': 'bar',
216
+ 'x': data[x].tolist(),
217
+ 'y': data[y_col].tolist(),
218
+ 'name': y_col,
219
+ 'marker': {'color': colors[idx % len(colors)]}
220
+ }
221
+ else: # horizontal
222
+ trace = {
223
+ 'type': 'bar',
224
+ 'x': data[y_col].tolist(),
225
+ 'y': data[x].tolist(),
226
+ 'orientation': 'h',
227
+ 'name': y_col,
228
+ 'marker': {'color': colors[idx % len(colors)]}
229
+ }
230
+ traces.append(trace)
231
+
232
+ return {
233
+ 'data': traces,
234
+ 'layout': {
235
+ 'title': title or '',
236
+ 'barmode': barmode,
237
+ 'xaxis': {'title': x if orientation == 'v' else 'Value'},
238
+ 'yaxis': {'title': 'Value' if orientation == 'v' else x}
239
+ }
240
+ }
241
+
242
+
243
+ def _dataframe_to_histogram(
244
+ data: Any,
245
+ x: str,
246
+ nbins: int = 30,
247
+ show_mean: bool = True,
248
+ title: Optional[str] = None,
249
+ ) -> Dict[str, Any]:
250
+ """Convert DataFrame to Plotly histogram figure."""
251
+ from ..themes import get_active_theme
252
+
253
+ if x not in data.columns:
254
+ raise ValueError(f"Column '{x}' not found in DataFrame. Available columns: {list(data.columns)}")
255
+
256
+ theme = get_active_theme()
257
+ primary = theme['colors']['primary']
258
+ success = theme['colors']['success']
259
+
260
+ figure = {
261
+ 'data': [{
262
+ 'type': 'histogram',
263
+ 'x': data[x].tolist(),
264
+ 'nbinsx': nbins,
265
+ 'marker': {'color': primary, 'opacity': 0.75},
266
+ 'name': 'Distribution'
267
+ }],
268
+ 'layout': {
269
+ 'title': title or f'{x} Distribution',
270
+ 'xaxis': {'title': x},
271
+ 'yaxis': {'title': 'Frequency'},
272
+ 'showlegend': True
273
+ }
274
+ }
275
+
276
+ # Add mean line if requested
277
+ if show_mean:
278
+ mean_val = float(data[x].mean())
279
+ figure['layout']['shapes'] = [{
280
+ 'type': 'line',
281
+ 'x0': mean_val,
282
+ 'x1': mean_val,
283
+ 'y0': 0,
284
+ 'y1': 1,
285
+ 'yref': 'paper',
286
+ 'line': {'color': success, 'dash': 'dash', 'width': 2}
287
+ }]
288
+ figure['layout']['annotations'] = [{
289
+ 'x': mean_val,
290
+ 'y': 1,
291
+ 'yref': 'paper',
292
+ 'text': f'Mean: {mean_val:.2f}',
293
+ 'showarrow': False,
294
+ 'yshift': 10
295
+ }]
296
+
297
+ return figure
298
+
299
+
300
+ def _dataframe_to_pie(
301
+ data: Any,
302
+ labels: str,
303
+ values: str,
304
+ hole: float = 0.0,
305
+ title: Optional[str] = None,
306
+ ) -> Dict[str, Any]:
307
+ """Convert DataFrame to Plotly pie/donut figure."""
308
+ from ..themes import get_active_theme
309
+
310
+ if labels not in data.columns:
311
+ raise ValueError(f"Column '{labels}' not found in DataFrame. Available columns: {list(data.columns)}")
312
+ if values not in data.columns:
313
+ raise ValueError(f"Column '{values}' not found in DataFrame. Available columns: {list(data.columns)}")
314
+
315
+ theme = get_active_theme()
316
+ colors = [
317
+ theme['colors']['primary'],
318
+ theme['colors']['secondary'],
319
+ theme['colors']['success'],
320
+ theme['colors']['warning'],
321
+ theme['colors']['info'],
322
+ theme['colors']['error']
323
+ ]
324
+
325
+ return {
326
+ 'data': [{
327
+ 'type': 'pie',
328
+ 'labels': data[labels].tolist(),
329
+ 'values': data[values].tolist(),
330
+ 'hole': hole,
331
+ 'marker': {'colors': colors}
332
+ }],
333
+ 'layout': {
334
+ 'title': title or ''
335
+ }
336
+ }
337
+
338
+
134
339
  def smart_chart(
135
340
  data: Any,
136
341
  chart_type: str,
137
- # Line chart parameters
342
+ # Common chart parameters
138
343
  x: Optional[str] = None,
139
344
  y: Optional[Union[str, List[str]]] = None,
345
+ # Scatter plot parameters
346
+ size: Optional[str] = None,
347
+ text: Optional[str] = None,
348
+ # Bar chart parameters
349
+ orientation: str = 'v',
350
+ barmode: str = 'group',
351
+ # Histogram parameters
352
+ nbins: int = 30,
353
+ show_mean: bool = True,
354
+ # Pie chart parameters
355
+ labels: Optional[str] = None,
356
+ values: Optional[str] = None,
357
+ hole: float = 0.0,
140
358
  # Gauge chart parameters
141
359
  value_column: Optional[str] = None,
142
360
  min_value: Optional[float] = None,
@@ -165,18 +383,36 @@ def smart_chart(
165
383
  key: Optional[str] = None,
166
384
  ) -> Optional[Dict[str, Any]]:
167
385
  """
168
- Create a smart chart that automatically switches between line, gauge, and waterfall charts.
386
+ Create a smart chart that automatically converts DataFrames to Plotly charts.
169
387
 
170
388
  This is a wrapper component that simplifies chart creation by transforming DataFrames
171
389
  into appropriate Plotly figure configurations based on the specified chart_type.
172
390
 
173
391
  Args:
174
392
  data: pandas DataFrame containing the data to plot
175
- chart_type: Type of chart to create - 'line', 'gauge', or 'waterfall'
393
+ chart_type: Type of chart to create - 'line', 'scatter', 'bar', 'bar_horizontal',
394
+ 'histogram', 'pie', 'gauge', or 'waterfall'
395
+
396
+ # Common Chart Parameters
397
+ x: Column name for x-axis (required for line, scatter, bar, histogram charts)
398
+ y: Column name(s) for y-axis - string or list of strings (required for line, scatter, bar charts)
176
399
 
177
- # Line Chart Parameters
178
- x: Column name for x-axis (required for line charts)
179
- y: Column name(s) for y-axis - string or list of strings (required for line charts)
400
+ # Scatter Plot Parameters
401
+ size: Column name for marker sizes (optional)
402
+ text: Column name for hover text (optional)
403
+
404
+ # Bar Chart Parameters
405
+ orientation: 'v' for vertical (default) or 'h' for horizontal
406
+ barmode: 'group' (default), 'stack', or 'overlay'
407
+
408
+ # Histogram Parameters
409
+ nbins: Number of bins (default: 30)
410
+ show_mean: Show mean line on histogram (default: True)
411
+
412
+ # Pie Chart Parameters
413
+ labels: Column name for pie slice labels (required for pie charts)
414
+ values: Column name for pie slice values (required for pie charts)
415
+ hole: Hole size for donut chart (0.0-1.0, default: 0.0 for pie)
180
416
 
181
417
  # Gauge Chart Parameters
182
418
  value_column: Column name containing the value to display (required for gauge charts)
@@ -263,12 +499,33 @@ def smart_chart(
263
499
  raise ValueError("DataFrame cannot be None or empty")
264
500
 
265
501
  # Validate chart_type
266
- valid_types = ['line', 'gauge', 'waterfall']
502
+ valid_types = ['line', 'scatter', 'bar', 'bar_horizontal', 'histogram', 'pie', 'gauge', 'waterfall']
267
503
  if chart_type not in valid_types:
268
504
  raise ValueError(f"Invalid chart_type: '{chart_type}'. Must be one of: {', '.join(valid_types)}")
269
505
 
270
506
  # Route to appropriate transformer and validate required parameters
271
- if chart_type == 'line':
507
+ if chart_type == 'scatter':
508
+ if x is None or y is None:
509
+ raise ValueError("Scatter chart requires 'x' and 'y' parameters")
510
+ figure = _dataframe_to_scatter(data, x, y, size, color, text, title)
511
+
512
+ elif chart_type in ['bar', 'bar_horizontal']:
513
+ if x is None or y is None:
514
+ raise ValueError("Bar chart requires 'x' and 'y' parameters")
515
+ orient = 'h' if chart_type == 'bar_horizontal' else 'v'
516
+ figure = _dataframe_to_bar(data, x, y, orient, barmode, title)
517
+
518
+ elif chart_type == 'histogram':
519
+ if x is None:
520
+ raise ValueError("Histogram requires 'x' parameter")
521
+ figure = _dataframe_to_histogram(data, x, nbins, show_mean, title)
522
+
523
+ elif chart_type == 'pie':
524
+ if labels is None or values is None:
525
+ raise ValueError("Pie chart requires 'labels' and 'values' parameters")
526
+ figure = _dataframe_to_pie(data, labels, values, hole, title)
527
+
528
+ elif chart_type == 'line':
272
529
  if x is None or y is None:
273
530
  raise ValueError("Line chart requires 'x' and 'y' parameters")
274
531
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: streamlit-react-components
3
- Version: 1.0.6
3
+ Version: 1.0.7
4
4
  Summary: Reusable React-based Streamlit components with Tailwind CSS styling
5
5
  License: MIT
6
6
  Project-URL: Homepage, https://github.com/your-org/streamlit-react-components
@@ -1,8 +1,8 @@
1
- streamlit_react_components/__init__.py,sha256=ueHhcoa5XS_P3nghY80-7tAdDXR_syIdfZZ4rJYdhPk,745
1
+ streamlit_react_components/__init__.py,sha256=gWfsQNrQ_OwZwy4bDPSBp80psvvSdlbwt64J_ZLmuio,745
2
2
  streamlit_react_components/themes.py,sha256=zAp314i-WZMP5WZjOKrFoKmTd84tLyuyICKOJAz4rB0,35225
3
3
  streamlit_react_components/_frontend/index.css,sha256=RaHZDlv7w2l_JlpyaVjuFIqaVBDXdpVfXE-Ugry8sPw,28362
4
4
  streamlit_react_components/_frontend/index.html,sha256=CjBEtVYlgT_q06BE16EHFrUrJZ6z0MCprrTg7qgAWzw,381
5
- streamlit_react_components/_frontend/index.js,sha256=RSQT0P7opMQfDecXk7jb2xH04iuRR-YvrPClJhIhc2s,5223994
5
+ streamlit_react_components/_frontend/index.js,sha256=FUVImO7iqBR60BpMziYAoopRDqUedK4Y554pu-TrXKQ,5224070
6
6
  streamlit_react_components/common/__init__.py,sha256=l5BfFZdqjrdLG3sJnJrgahEiTgoBX92_aE0wp2tn_w0,606
7
7
  streamlit_react_components/common/button_group.py,sha256=TlOmJRCPEb4MWMmKKiTe2a8ENElC_YB83mHp_WCFDas,2672
8
8
  streamlit_react_components/common/chart_legend.py,sha256=ylul6Os5A89S8fIJs7XpBdIk6uWijg_zwR1eB7u9wSw,1824
@@ -11,7 +11,7 @@ streamlit_react_components/common/metric_row.py,sha256=84KmKq_ECJ6kTozDSVTVVxN0G
11
11
  streamlit_react_components/common/panel.py,sha256=iMcB5RF0SnU4yZHsvQQRuiNPnwQ6m8Epi56k2ziSjR8,1541
12
12
  streamlit_react_components/common/plotly_chart.py,sha256=YHY5kNBdyoEwNeJ4JPDh9DWDkN3zq8YgIncIMyu285A,9815
13
13
  streamlit_react_components/common/section_header.py,sha256=zgEW9Jlj48kiRB3_mYLk3a_oK7w55gnia_laMar_hOk,3430
14
- streamlit_react_components/common/smart_chart.py,sha256=6bgt9UTH5nE495HUfUcIbN3chs78q9pI63zBeCnNA_s,11843
14
+ streamlit_react_components/common/smart_chart.py,sha256=kYzdPTpgdJLfX6CUXuT8fUMRubN1DRZ9Zej-XFFgje4,20068
15
15
  streamlit_react_components/common/stat_card.py,sha256=-C-73nglpPFbtliK7JfZLQExc9N1wYsyTjDsTXHVjKQ,4758
16
16
  streamlit_react_components/common/step_indicator.py,sha256=RjoBdgWrhrUc2Rs5lTxs_4ocBuMJDI8ELNIQE_0Y7js,1817
17
17
  streamlit_react_components/form/__init__.py,sha256=jtK9U5HXCKo4KX7Sk5Yw1o0vis2nvh3pF1BM9oi7es8,278
@@ -19,7 +19,7 @@ streamlit_react_components/form/checkbox_group.py,sha256=fxp862q26l3DbT8OZUFtma2
19
19
  streamlit_react_components/form/form_select.py,sha256=E6QbwxdzHoCSwVBaLqU3HRGje_KGXI5FbKbu7cm9cos,2393
20
20
  streamlit_react_components/form/form_slider.py,sha256=6orYQejAAXmBN_Ffb5pl70k1mQGGnzKqtXNTVCm02h4,2479
21
21
  streamlit_react_components/form/radio_group.py,sha256=NgOo_C9wEV8sdpbR92bW2TdyhQYYSI498sLwxh5xT9E,2501
22
- streamlit_react_components-1.0.6.dist-info/METADATA,sha256=JWVssBdk3_Np295kjc3grnqXwBEhPBg5I7lwf07tcBA,21677
23
- streamlit_react_components-1.0.6.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
24
- streamlit_react_components-1.0.6.dist-info/top_level.txt,sha256=3JFrl15-Uewx3BFMSzqrBufF9GmTS1LDKfShmg0R9VE,27
25
- streamlit_react_components-1.0.6.dist-info/RECORD,,
22
+ streamlit_react_components-1.0.7.dist-info/METADATA,sha256=ZlAWTDPxupS10b0twR-pgLzMeLLCXWf4uyD5UTzwU2s,21677
23
+ streamlit_react_components-1.0.7.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
24
+ streamlit_react_components-1.0.7.dist-info/top_level.txt,sha256=3JFrl15-Uewx3BFMSzqrBufF9GmTS1LDKfShmg0R9VE,27
25
+ streamlit_react_components-1.0.7.dist-info/RECORD,,