| 718 | |
| 719 | |
| 720 | class DatabricksRenderer(ExternalRenderer): |
| 721 | def __init__( |
| 722 | self, |
| 723 | config=None, |
| 724 | auto_play=False, |
| 725 | post_script=None, |
| 726 | animation_opts=None, |
| 727 | include_plotlyjs="cdn", |
| 728 | ): |
| 729 | self.config = config |
| 730 | self.auto_play = auto_play |
| 731 | self.post_script = post_script |
| 732 | self.animation_opts = animation_opts |
| 733 | self.include_plotlyjs = include_plotlyjs |
| 734 | self._displayHTML = None |
| 735 | |
| 736 | @property |
| 737 | def displayHTML(self): |
| 738 | import inspect |
| 739 | |
| 740 | if self._displayHTML is None: |
| 741 | for frame in inspect.getouterframes(inspect.currentframe()): |
| 742 | global_names = set(frame.frame.f_globals) |
| 743 | # Check for displayHTML plus a few others to reduce chance of a false |
| 744 | # hit. |
| 745 | if all(v in global_names for v in ["displayHTML", "display", "spark"]): |
| 746 | self._displayHTML = frame.frame.f_globals["displayHTML"] |
| 747 | break |
| 748 | |
| 749 | if self._displayHTML is None: |
| 750 | raise EnvironmentError( |
| 751 | """ |
| 752 | Unable to detect the Databricks displayHTML function. The 'databricks' renderer is only |
| 753 | supported when called from within the Databricks notebook environment.""" |
| 754 | ) |
| 755 | |
| 756 | return self._displayHTML |
| 757 | |
| 758 | def render(self, fig_dict): |
| 759 | from plotly.io import to_html |
| 760 | |
| 761 | html = to_html( |
| 762 | fig_dict, |
| 763 | config=self.config, |
| 764 | auto_play=self.auto_play, |
| 765 | include_plotlyjs=self.include_plotlyjs, |
| 766 | include_mathjax="cdn", |
| 767 | post_script=self.post_script, |
| 768 | full_html=True, |
| 769 | animation_opts=self.animation_opts, |
| 770 | default_width="100%", |
| 771 | default_height="100%", |
| 772 | validate=False, |
| 773 | ) |
| 774 | |
| 775 | # displayHTML is a Databricks notebook built-in function |
| 776 | self.displayHTML(html) |
| 777 | |