Coverage for src/pycse/plotly.py: 0.00%

16 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-23 16:23 -0400

1"""Module for using plotly with orgmode. 

2 

3This monkey-patches go.Figure.show to provide a png image for org-mode, and an 

4html file that is saved that you can click on in org-mode to see the interactive 

5version. 

6 

7Example: 

8 

9 import pycse.plotly 

10 import plotly.graph_objects as go 

11 

12 # Sample data 

13 x = [0, 1, 2, 3, 4] 

14 y = [0, 1, 4, 9, 16] 

15 

16 # Create a figure 

17 fig = go.Figure( 

18 data=go.Scatter(x=x, y=y, mode='lines+markers', name='y = x^2'), 

19 layout=go.Layout( 

20 title='Simple Line Plot', 

21 xaxis_title='x', 

22 yaxis_title='y', 

23 ) 

24 ) 

25 

26 # Show the plot 

27 fig.show() 

28 

29""" 

30 

31import os 

32 

33from hashlib import md5 

34 

35from IPython import display 

36 

37import plotly.graph_objects as go 

38import plotly.io as pio 

39 

40 

41def myshow(self, *args, **kwargs): 

42 """Make a PNG image to display for plotly.""" 

43 html = pio.to_html(self) 

44 mhash = md5(html.encode("utf-8")).hexdigest() 

45 if not os.path.isdir(".ob-jupyter"): 

46 os.mkdir(".ob-jupyter") 

47 fhtml = os.path.join(".ob-jupyter", mhash + ".html") 

48 

49 with open(fhtml, "w", encoding="utf-8") as f: 

50 f.write(html) 

51 

52 display.FileLink(fhtml, result_html_suffix="") 

53 

54 return display.Image(pio.to_image(self, "png", engine="kaleido")) 

55 

56 

57go.Figure.show = myshow