Skip to content

Commit 5784648

Browse files
committed
Add support for JSON files
1 parent 5e117ad commit 5784648

File tree

6 files changed

+646
-1
lines changed

6 files changed

+646
-1
lines changed

.github/workflows/deploy-pages.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ jobs:
2525
- name: Checkout
2626
uses: actions/checkout@v4
2727

28+
- name: Install uv (Python package manager)
29+
run: |
30+
curl -LsSf https://astral.sh/uv/install.sh | sh
31+
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
32+
33+
- name: Convert JSON files to HTML
34+
run: |
35+
if [ -d "json" ] && [ "$(ls -A json/*.json 2>/dev/null)" ]; then
36+
chmod +x convert_json_to_html.py
37+
~/.cargo/bin/uv run convert_json_to_html.py
38+
else
39+
echo "No JSON files found in json/ directory, skipping conversion"
40+
fi
41+
2842
- name: Disable Chart Studio showLink
2943
run: |
3044
chmod +x .github/scripts/disable-showlink.sh

README.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ Host your Plotly Chart Studio visualizations online for free using GitHub Pages.
1313

1414
## What This Does
1515

16-
Upload your Chart Studio HTML exports, and they'll be automatically published online:
16+
Upload your Chart Studio HTML exports or JSON data files, and they'll be automatically published online:
1717
- An **index page** listing all your Plotly charts is created at `https://yourusername.github.io/your-project/`
1818
- Each chart gets its own web address: `https://yourusername.github.io/your-project/my-chart.html`
19+
- **NEW:** Support for JSON exports - just place Chart Studio JSON files in the `json/` directory and they'll be automatically converted to HTML
1920

2021
Perfect for sharing interactive Plotly visualizations with your team or embedding in presentations!
2122

@@ -65,6 +66,14 @@ You can add files directly on GitHub (easiest) or use GitHub Desktop:
6566
4. In GitHub Desktop, write a description and click **Commit to main**
6667
5. Click **Push origin** to upload
6768

69+
**Option C: Upload JSON Files (Alternative)**
70+
If you have JSON exports from Chart Studio instead of HTML files:
71+
1. Click on the `json` folder (or create it if it doesn't exist)
72+
2. Click **Add file****Upload files**
73+
3. Drag and drop your Chart Studio JSON files (e.g., `chart_data.json` files)
74+
4. Click **Commit changes** at the bottom
75+
5. The GitHub Action will automatically convert these to HTML files in the `charts` folder
76+
6877
### Step 5: Wait for Publishing
6978

7079
After uploading files:
@@ -185,3 +194,60 @@ uv run poll_all_charts.py
185194
The script will download all your charts and save them as HTML files in the `charts/` folder. Then just commit and push the changes to publish them on GitHub Pages!
186195

187196
**Note:** This script uses Plotly.js version 1.58.5 to ensure compatibility and consistent rendering across all your charts.
197+
198+
## Working with JSON Exports
199+
200+
If you have Chart Studio JSON exports (such as `chart_data.json` files from bulk exports), this repository can automatically convert them to HTML files.
201+
202+
### Why Use JSON Files?
203+
204+
- Bulk exports from Chart Studio often come as JSON files
205+
- JSON files are more portable and easier to version control
206+
- You can modify chart data programmatically before converting to HTML
207+
- Smaller file sizes compared to full HTML files
208+
209+
### How to Use JSON Files
210+
211+
**Option 1: Manual Conversion (Local)**
212+
213+
1. Place your JSON files in the `json/` directory
214+
2. Run the conversion script locally:
215+
```bash
216+
./convert_json_to_html.py
217+
```
218+
3. The script will create HTML files in the `charts/` directory
219+
220+
**Option 2: Automatic Conversion (GitHub)**
221+
222+
1. Upload your JSON files to the `json/` folder in your repository
223+
2. Commit and push the changes
224+
3. GitHub Actions will automatically:
225+
- Detect the JSON files
226+
- Convert them to HTML using the same Plotly.js version (1.58.5)
227+
- Include them in your published site
228+
229+
### JSON File Format
230+
231+
The JSON files should match the Chart Studio export format:
232+
233+
```json
234+
{
235+
"data": [
236+
{
237+
"type": "scatter",
238+
"x": [1, 2, 3],
239+
"y": [2, 4, 6],
240+
"mode": "lines+markers"
241+
}
242+
],
243+
"layout": {
244+
"title": {
245+
"text": "My Chart"
246+
},
247+
"xaxis": {"title": {"text": "X Axis"}},
248+
"yaxis": {"title": {"text": "Y Axis"}}
249+
}
250+
}
251+
```
252+
253+
The conversion script reads the `data` and `layout` fields and generates standalone HTML files with embedded Plotly.js.

convert_json_to_html.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env -S uv run --script
2+
#
3+
# /// script
4+
# requires-python = ">=3.12"
5+
# dependencies = []
6+
# ///
7+
8+
import json
9+
import os
10+
import sys
11+
12+
def convert_json_to_html(json_path, output_path):
13+
"""Convert a Chart Studio JSON export to standalone HTML"""
14+
with open(json_path, 'r') as f:
15+
chart_data = json.load(f)
16+
17+
# Extract data and layout from the JSON
18+
data_json = json.dumps(chart_data.get('data', []))
19+
layout_json = json.dumps(chart_data.get('layout', {}))
20+
21+
# Generate standalone HTML with Plotly.js 1.58.5
22+
html_template = f"""<html>
23+
<head><meta charset="utf-8" /></head>
24+
<body>
25+
<div id="plotly-div"></div>
26+
<script src="https://cdn.plot.ly/plotly-1.58.5.min.js"></script>
27+
<script>
28+
var data = {data_json};
29+
var layout = {layout_json};
30+
Plotly.newPlot('plotly-div', data, layout);
31+
</script>
32+
</body>
33+
</html>"""
34+
35+
with open(output_path, 'w') as f:
36+
f.write(html_template)
37+
38+
return output_path
39+
40+
def main():
41+
# Convert all JSON files in the json/ directory
42+
json_dir = "json"
43+
charts_dir = "charts"
44+
45+
if not os.path.exists(json_dir):
46+
print(f"❌ Directory '{json_dir}' does not exist")
47+
sys.exit(1)
48+
49+
os.makedirs(charts_dir, exist_ok=True)
50+
51+
json_files = [f for f in os.listdir(json_dir) if f.endswith('.json')]
52+
53+
if not json_files:
54+
print(f"No JSON files found in '{json_dir}' directory")
55+
return
56+
57+
print(f"Found {len(json_files)} JSON file(s) to convert")
58+
59+
for json_file in json_files:
60+
json_path = os.path.join(json_dir, json_file)
61+
html_filename = json_file.replace('.json', '.html')
62+
html_path = os.path.join(charts_dir, html_filename)
63+
64+
try:
65+
convert_json_to_html(json_path, html_path)
66+
print(f"✅ Converted {json_file}{html_filename}")
67+
except Exception as e:
68+
print(f"❌ Failed to convert {json_file}: {e}")
69+
70+
if __name__ == "__main__":
71+
main()

json/example-chart-2.json

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"data": [
3+
{
4+
"meta": {
5+
"columnNames": {
6+
"labels": "A",
7+
"values": "D"
8+
}
9+
},
10+
"mode": "markers",
11+
"type": "pie",
12+
"labelssrc": "kiwisky:10:e2d9e3",
13+
"labels": [
14+
"Club",
15+
"Real Madrid CF",
16+
"FC Bayern M\u00fcnchen",
17+
"FC Barcelona",
18+
"Manchester United",
19+
"Juventus",
20+
"Liverpool FC",
21+
"AC Milan",
22+
"SL Benfica",
23+
"FC Porto",
24+
"AFC Ajax",
25+
"Chelsea FC",
26+
"FC Internazionale Milano",
27+
"Nottingham Forest FC"
28+
],
29+
"valuessrc": "kiwisky:10:38e1a2",
30+
"values": [
31+
"Titles",
32+
"14",
33+
"6",
34+
"5",
35+
"3",
36+
"2",
37+
"6",
38+
"7",
39+
"2",
40+
"2",
41+
"4",
42+
"2",
43+
"3",
44+
"2"
45+
]
46+
}
47+
],
48+
"frames": [],
49+
"layout": {
50+
"title": {
51+
"x": 0.05,
52+
"font": {
53+
"size": 35,
54+
"family": "Droid Sans"
55+
},
56+
"text": "<b>Vyhran\u00e9 tituly (klub)</b>"
57+
},
58+
"xaxis": {
59+
"range": [
60+
-1,
61+
6
62+
],
63+
"autorange": true
64+
},
65+
"yaxis": {
66+
"range": [
67+
-1,
68+
4
69+
],
70+
"autorange": true
71+
},
72+
"legend": {
73+
"x": 0.9199999999999999,
74+
"y": 1,
75+
"font": {
76+
"size": 25
77+
},
78+
"borderwidth": 3
79+
},
80+
"margin": {
81+
"l": 50
82+
},
83+
"autosize": true,
84+
"paper_bgcolor": "rgb(227, 227, 227)"
85+
}
86+
}

0 commit comments

Comments
 (0)