|
10 | 10 | from reflex.components.core.cond import color_mode_cond |
11 | 11 | from reflex.event import EventHandler, no_args_event_spec |
12 | 12 | from reflex.utils import console |
| 13 | +from reflex.utils.imports import ImportDict, ImportVar |
13 | 14 | from reflex.vars.base import LiteralVar, Var |
14 | 15 |
|
15 | 16 | try: |
@@ -278,3 +279,237 @@ def _render(self): |
278 | 279 | # Spread the figure dict over props, nothing to merge. |
279 | 280 | tag.special_props.append(Var(_js_expr=f"{{...{figure!s}}}")) |
280 | 281 | return tag |
| 282 | + |
| 283 | + |
| 284 | +CREATE_PLOTLY_COMPONENT: ImportDict = { |
| 285 | + "react-plotly.js": [ |
| 286 | + ImportVar( |
| 287 | + tag="createPlotlyComponent", |
| 288 | + is_default=True, |
| 289 | + package_path="/factory", |
| 290 | + ), |
| 291 | + ] |
| 292 | +} |
| 293 | + |
| 294 | + |
| 295 | +def dynamic_plotly_import(name: str, package: str) -> str: |
| 296 | + """Create a dynamic import for a plotly component. |
| 297 | +
|
| 298 | + Args: |
| 299 | + name: The name of the component. |
| 300 | + package: The package path of the component. |
| 301 | +
|
| 302 | + Returns: |
| 303 | + The dynamic import for the plotly component. |
| 304 | + """ |
| 305 | + return f""" |
| 306 | +const {name} = dynamic(() => import('{package}').then(mod => createPlotlyComponent(mod)), {{ssr: false}}) |
| 307 | +""" |
| 308 | + |
| 309 | + |
| 310 | +class PlotlyBasic(Plotly): |
| 311 | + """Display a basic plotly graph.""" |
| 312 | + |
| 313 | + tag: str = "BasicPlotlyPlot" |
| 314 | + |
| 315 | + |
| 316 | + |
| 317 | + lib_dependencies: list[ str] = [ "[email protected]"] |
| 318 | + |
| 319 | + def add_imports(self) -> ImportDict | list[ImportDict]: |
| 320 | + """Add imports for the plotly basic component. |
| 321 | +
|
| 322 | + Returns: |
| 323 | + The imports for the plotly basic component. |
| 324 | + """ |
| 325 | + return CREATE_PLOTLY_COMPONENT |
| 326 | + |
| 327 | + def _get_dynamic_imports(self) -> str: |
| 328 | + """Get the dynamic imports for the plotly basic component. |
| 329 | +
|
| 330 | + Returns: |
| 331 | + The dynamic imports for the plotly basic component. |
| 332 | + """ |
| 333 | + return dynamic_plotly_import(self.tag, "plotly.js-basic-dist-min") |
| 334 | + |
| 335 | + |
| 336 | +class PlotlyCartesian(Plotly): |
| 337 | + """Display a plotly cartesian graph.""" |
| 338 | + |
| 339 | + tag: str = "CartesianPlotlyPlot" |
| 340 | + |
| 341 | + |
| 342 | + |
| 343 | + lib_dependencies: list[ str] = [ "[email protected]"] |
| 344 | + |
| 345 | + def add_imports(self) -> ImportDict | list[ImportDict]: |
| 346 | + """Add imports for the plotly cartesian component. |
| 347 | +
|
| 348 | + Returns: |
| 349 | + The imports for the plotly cartesian component. |
| 350 | + """ |
| 351 | + return CREATE_PLOTLY_COMPONENT |
| 352 | + |
| 353 | + def _get_dynamic_imports(self) -> str: |
| 354 | + """Get the dynamic imports for the plotly cartesian component. |
| 355 | +
|
| 356 | + Returns: |
| 357 | + The dynamic imports for the plotly cartesian component. |
| 358 | + """ |
| 359 | + return dynamic_plotly_import(self.tag, "plotly.js-cartesian-dist-min") |
| 360 | + |
| 361 | + |
| 362 | +class PlotlyGeo(Plotly): |
| 363 | + """Display a plotly geo graph.""" |
| 364 | + |
| 365 | + tag: str = "GeoPlotlyPlot" |
| 366 | + |
| 367 | + |
| 368 | + |
| 369 | + lib_dependencies: list[ str] = [ "[email protected]"] |
| 370 | + |
| 371 | + def add_imports(self) -> ImportDict | list[ImportDict]: |
| 372 | + """Add imports for the plotly geo component. |
| 373 | +
|
| 374 | + Returns: |
| 375 | + The imports for the plotly geo component. |
| 376 | + """ |
| 377 | + return CREATE_PLOTLY_COMPONENT |
| 378 | + |
| 379 | + def _get_dynamic_imports(self) -> str: |
| 380 | + """Get the dynamic imports for the plotly geo component. |
| 381 | +
|
| 382 | + Returns: |
| 383 | + The dynamic imports for the plotly geo component. |
| 384 | + """ |
| 385 | + return dynamic_plotly_import(self.tag, "plotly.js-geo-dist-min") |
| 386 | + |
| 387 | + |
| 388 | +class PlotlyGl3d(Plotly): |
| 389 | + """Display a plotly 3d graph.""" |
| 390 | + |
| 391 | + tag: str = "Gl3dPlotlyPlot" |
| 392 | + |
| 393 | + |
| 394 | + |
| 395 | + lib_dependencies: list[ str] = [ "[email protected]"] |
| 396 | + |
| 397 | + def add_imports(self) -> ImportDict | list[ImportDict]: |
| 398 | + """Add imports for the plotly 3d component. |
| 399 | +
|
| 400 | + Returns: |
| 401 | + The imports for the plotly 3d component. |
| 402 | + """ |
| 403 | + return CREATE_PLOTLY_COMPONENT |
| 404 | + |
| 405 | + def _get_dynamic_imports(self) -> str: |
| 406 | + """Get the dynamic imports for the plotly 3d component. |
| 407 | +
|
| 408 | + Returns: |
| 409 | + The dynamic imports for the plotly 3d component. |
| 410 | + """ |
| 411 | + return dynamic_plotly_import(self.tag, "plotly.js-gl3d-dist-min") |
| 412 | + |
| 413 | + |
| 414 | +class PlotlyGl2d(Plotly): |
| 415 | + """Display a plotly 2d graph.""" |
| 416 | + |
| 417 | + tag: str = "Gl2dPlotlyPlot" |
| 418 | + |
| 419 | + |
| 420 | + |
| 421 | + lib_dependencies: list[ str] = [ "[email protected]"] |
| 422 | + |
| 423 | + def add_imports(self) -> ImportDict | list[ImportDict]: |
| 424 | + """Add imports for the plotly 2d component. |
| 425 | +
|
| 426 | + Returns: |
| 427 | + The imports for the plotly 2d component. |
| 428 | + """ |
| 429 | + return CREATE_PLOTLY_COMPONENT |
| 430 | + |
| 431 | + def _get_dynamic_imports(self) -> str: |
| 432 | + """Get the dynamic imports for the plotly 2d component. |
| 433 | +
|
| 434 | + Returns: |
| 435 | + The dynamic imports for the plotly 2d component. |
| 436 | + """ |
| 437 | + return dynamic_plotly_import(self.tag, "plotly.js-gl2d-dist-min") |
| 438 | + |
| 439 | + |
| 440 | +class PlotlyMapbox(Plotly): |
| 441 | + """Display a plotly mapbox graph.""" |
| 442 | + |
| 443 | + tag: str = "MapboxPlotlyPlot" |
| 444 | + |
| 445 | + |
| 446 | + |
| 447 | + lib_dependencies: list[ str] = [ "[email protected]"] |
| 448 | + |
| 449 | + def add_imports(self) -> ImportDict | list[ImportDict]: |
| 450 | + """Add imports for the plotly mapbox component. |
| 451 | +
|
| 452 | + Returns: |
| 453 | + The imports for the plotly mapbox component. |
| 454 | + """ |
| 455 | + return CREATE_PLOTLY_COMPONENT |
| 456 | + |
| 457 | + def _get_dynamic_imports(self) -> str: |
| 458 | + """Get the dynamic imports for the plotly mapbox component. |
| 459 | +
|
| 460 | + Returns: |
| 461 | + The dynamic imports for the plotly mapbox component. |
| 462 | + """ |
| 463 | + return dynamic_plotly_import(self.tag, "plotly.js-mapbox-dist-min") |
| 464 | + |
| 465 | + |
| 466 | +class PlotlyFinance(Plotly): |
| 467 | + """Display a plotly finance graph.""" |
| 468 | + |
| 469 | + tag: str = "FinancePlotlyPlot" |
| 470 | + |
| 471 | + |
| 472 | + |
| 473 | + lib_dependencies: list[ str] = [ "[email protected]"] |
| 474 | + |
| 475 | + def add_imports(self) -> ImportDict | list[ImportDict]: |
| 476 | + """Add imports for the plotly finance component. |
| 477 | +
|
| 478 | + Returns: |
| 479 | + The imports for the plotly finance component. |
| 480 | + """ |
| 481 | + return CREATE_PLOTLY_COMPONENT |
| 482 | + |
| 483 | + def _get_dynamic_imports(self) -> str: |
| 484 | + """Get the dynamic imports for the plotly finance component. |
| 485 | +
|
| 486 | + Returns: |
| 487 | + The dynamic imports for the plotly finance component. |
| 488 | + """ |
| 489 | + return dynamic_plotly_import(self.tag, "plotly.js-finance-dist-min") |
| 490 | + |
| 491 | + |
| 492 | +class PlotlyStrict(Plotly): |
| 493 | + """Display a plotly strict graph.""" |
| 494 | + |
| 495 | + tag: str = "StrictPlotlyPlot" |
| 496 | + |
| 497 | + |
| 498 | + |
| 499 | + lib_dependencies: list[ str] = [ "[email protected]"] |
| 500 | + |
| 501 | + def add_imports(self) -> ImportDict | list[ImportDict]: |
| 502 | + """Add imports for the plotly strict component. |
| 503 | +
|
| 504 | + Returns: |
| 505 | + The imports for the plotly strict component. |
| 506 | + """ |
| 507 | + return CREATE_PLOTLY_COMPONENT |
| 508 | + |
| 509 | + def _get_dynamic_imports(self) -> str: |
| 510 | + """Get the dynamic imports for the plotly strict component. |
| 511 | +
|
| 512 | + Returns: |
| 513 | + The dynamic imports for the plotly strict component. |
| 514 | + """ |
| 515 | + return dynamic_plotly_import(self.tag, "plotly.js-strict-dist-min") |
0 commit comments