Skip to content

Commit b4ec570

Browse files
authored
🐛 Fix: add code to handle plotly plots generated with R (#96)
* 🐛 Fix: add code to handle plotly plots generated with R in streamlit and quarto reports * 🎨 Style: delete config file and format code with black
1 parent eb399be commit b4ec570

File tree

3 files changed

+112
-8
lines changed

3 files changed

+112
-8
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"visdat": {
3+
"ac2421aa47": ["function () ", "plotlyVisDat"]
4+
},
5+
"cur_data": "ac2421aa47",
6+
"attrs": {
7+
"ac2421aa47": {
8+
"x": {},
9+
"y": {},
10+
"mode": "markers",
11+
"alpha_stroke": 1,
12+
"sizes": [10, 100],
13+
"spans": [1, 20],
14+
"type": "scatter"
15+
}
16+
},
17+
"layout": {
18+
"margin": {
19+
"b": 40,
20+
"l": 60,
21+
"t": 25,
22+
"r": 10
23+
},
24+
"xaxis": {
25+
"domain": [0, 1],
26+
"automargin": true,
27+
"title": "mpg"
28+
},
29+
"yaxis": {
30+
"domain": [0, 1],
31+
"automargin": true,
32+
"title": "hp"
33+
},
34+
"hovermode": "closest",
35+
"showlegend": false
36+
},
37+
"source": "A",
38+
"config": {
39+
"modeBarButtonsToAdd": ["hoverclosest", "hovercompare"],
40+
"showSendToCloud": false
41+
},
42+
"data": [
43+
{
44+
"x": [21, 21, 22.800000000000001, 21.399999999999999, 18.699999999999999, 18.100000000000001, 14.300000000000001, 24.399999999999999, 22.800000000000001, 19.199999999999999, 17.800000000000001, 16.399999999999999, 17.300000000000001, 15.199999999999999, 10.4, 10.4, 14.699999999999999, 32.399999999999999, 30.399999999999999, 33.899999999999999, 21.5, 15.5, 15.199999999999999, 13.300000000000001, 19.199999999999999, 27.300000000000001, 26, 30.399999999999999, 15.800000000000001, 19.699999999999999, 15, 21.399999999999999],
45+
"y": [110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180, 205, 215, 230, 66, 52, 65, 97, 150, 150, 245, 175, 66, 91, 113, 264, 175, 335, 109],
46+
"mode": "markers",
47+
"type": "scatter",
48+
"marker": {
49+
"color": "rgba(31,119,180,1)",
50+
"line": {
51+
"color": "rgba(31,119,180,1)"
52+
}
53+
},
54+
"error_y": {
55+
"color": "rgba(31,119,180,1)"
56+
},
57+
"error_x": {
58+
"color": "rgba(31,119,180,1)"
59+
},
60+
"line": {
61+
"color": "rgba(31,119,180,1)"
62+
},
63+
"xaxis": "x",
64+
"yaxis": "y",
65+
"frame": null
66+
}
67+
],
68+
"highlight": {
69+
"on": "plotly_click",
70+
"persistent": false,
71+
"dynamic": false,
72+
"selectize": false,
73+
"opacityDim": 0.20000000000000001,
74+
"selected": {
75+
"opacity": 1
76+
},
77+
"debounce": 0
78+
},
79+
"shinyEvents": ["plotly_hover", "plotly_click", "plotly_selected", "plotly_relayout", "plotly_brushed", "plotly_brushing", "plotly_clickannotation", "plotly_doubleclick", "plotly_deselect", "plotly_afterplot", "plotly_sunburstclick"],
80+
"base_url": "https://plot.ly"
81+
}

src/vuegen/quarto_reportview.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -571,14 +571,25 @@ def _generate_plot_code(self, plot, output_file="") -> str:
571571
else: # If it's a local file
572572
plot_code += f"""
573573
with open('{(Path("..") / plot.file_path).as_posix()}', 'r') as plot_file:
574-
plot_json = plot_file.read()\n"""
574+
plot_json = json.load(plot_file)\n"""
575575
# Add specific code for each visualization tool
576576
if plot.plot_type == r.PlotType.PLOTLY:
577577
plot_code += """
578-
fig_plotly = pio.from_json(plot_json)
578+
# Keep only 'data' and 'layout' sections
579+
plot_json = {key: plot_json[key] for key in plot_json if key in ['data', 'layout']}\n
580+
# Remove 'frame' section in 'data'
581+
plot_json['data'] = [{k: v for k, v in entry.items() if k != 'frame'} for entry in plot_json.get('data', [])]\n
582+
# Convert JSON to string
583+
plot_json_str = json.dumps(plot_json)\n
584+
# Create the plotly plot
585+
fig_plotly = pio.from_json(plot_json_str)
579586
fig_plotly.update_layout(width=950, height=500)\n"""
580587
elif plot.plot_type == r.PlotType.ALTAIR:
581-
plot_code += """fig_altair = alt.Chart.from_json(plot_json).properties(width=900, height=400)"""
588+
plot_code += """
589+
# Convert JSON to string
590+
plot_json_str = json.dumps(plot_json)\n
591+
# Create the plotly plot
592+
fig_altair = alt.Chart.from_json(plot_json_str).properties(width=900, height=400)\n"""
582593
elif plot.plot_type == r.PlotType.INTERACTIVE_NETWORK:
583594
# Generate the HTML embedding for interactive networks
584595
if is_url(plot.file_path) and plot.file_path.endswith(".html"):
@@ -866,8 +877,16 @@ def _generate_component_imports(self, component: r.Component) -> List[str]:
866877
# Dictionary to hold the imports for each component type
867878
components_imports = {
868879
"plot": {
869-
r.PlotType.ALTAIR: ["import altair as alt", "import requests"],
870-
r.PlotType.PLOTLY: ["import plotly.io as pio", "import requests"],
880+
r.PlotType.ALTAIR: [
881+
"import altair as alt",
882+
"import requests",
883+
"import json",
884+
],
885+
r.PlotType.PLOTLY: [
886+
"import plotly.io as pio",
887+
"import requests",
888+
"import json",
889+
],
871890
},
872891
"dataframe": [
873892
"import pandas as pd",

src/vuegen/streamlit_reportview.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,13 @@ def _generate_plot_code(self, plot) -> str:
570570

571571
# Add specific code for each visualization tool
572572
if plot.plot_type == r.PlotType.PLOTLY:
573-
plot_code += "st.plotly_chart(plot_json, use_container_width=True)\n"
573+
plot_code += """
574+
# Keep only 'data' and 'layout' sections
575+
plot_json = {key: plot_json[key] for key in plot_json if key in ['data', 'layout']}
576+
577+
# Remove 'frame' section in 'data'
578+
plot_json['data'] = [{k: v for k, v in entry.items() if k != 'frame'} for entry in plot_json.get('data', [])]
579+
st.plotly_chart(plot_json, use_container_width=True)\n"""
574580

575581
elif plot.plot_type == r.PlotType.ALTAIR:
576582
plot_code += """
@@ -987,5 +993,3 @@ def _generate_component_imports(self, component: r.Component) -> List[str]:
987993

988994
# Return the list of import statements
989995
return component_imports
990-
991-
return component_imports

0 commit comments

Comments
 (0)