Just an update - I did figure out a serverless - and rather hacky - way to do this, and just wanted to share for anyone interested or if people want to suggest better ways (surely there are). Steps are outlined in more detail below but the short summary of the workflow is that from the python script you extract whatever data from your model you are looking to visualize as a JSON string, then search and replace some placeholder text in your js file (this is to avoid having to load external files later and thus the CORS issue). Then you call d3 and the updates js file from your html file as you normally would.
In javascript file:
Create a temporary placeholder for your data
var json_str = 'placeholder';
var data = JSON.parse(json_str);
var root = d3.hierarchy(data);
In Python:
Collect your data from Revit, dump it as json string, search and replace js file, load output window.
json_str = json.dumps(your_data)
js_path = <PATH TO YOUR JS FILE>
with open(js_path,'r') as f:
txt = f.read()
txt = txt.replace('placeholder', json_str)
new_path = <PATH TO NEW FILE> #This is optional for if you don't want to override the original js file
with open(new_path,'w') as f:
f.write(txt)
output = script.get_output()
output.open_page(<YOUR HTML FILE PATH>)
In HTML:
Load d3 and any other libraries in the header, and load your javascript in the body. I found that I needed to pass absolute file paths for my local css and js files since the output window doesn’t open from the directory of where your tool lives. I was also using an older version of d3, but you can probably use a newer one if you make sure to update the browser compatibility, etc.
<!-- In header -->
<link rel="stylesheet" type="text/css" href="<ABSOLUTE FILE PATH TO CSS>" >
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.1.0/d3.min.js"></script>
<!-- In body-->
<script type="text/javascript" src="<ABSOLUTE FILE PATH TO JS FILE>"></script>