vizhub-core / vizhub-feedback

VizHub feedback issue tracker
32 stars 3 forks source link

How to refer to local js files in the html? #787

Open brettforbes opened 3 months ago

brettforbes commented 3 months ago

I have a standard html file, and a local js file ("stix_tree.js") I want to include in the body as a function.

I import it as shown below


    <script type="text/javascript" src="stix_tree.js"></script>

yet it throws an error in the console which shows the file is not found

image

How should one refer to local js files in the html file? I tried using a dot and slash but that didnt seem to work either. What am i doing wrong? Should there be some kind of help on these basic gotcha's?

curran commented 3 months ago

Thanks for reporting! Can you please share a link to the viz that is broken?

curran commented 3 months ago

I made a test case of importing a file the way you are doing it, and it seems to work.

https://vizhub.com/curran/f7f292e92a0942e7b0658f5d5325810f?edit=files&file=index.html&tabs=test.js%7Eindex.html

curran commented 3 months ago

Oh actually it broke when I put an underscore in the name! That must be a bug. Please try removing the underscore from the file name and see if it works.

brettforbes commented 3 months ago

hey Curran, thanks for helping.

I, I changed all of the filenames so there was no underscores, which is a gotcha for Python guys like myself.

but i still get 2 errors about import in the results webpage, yet the thing works

What am i doing wrong? Are those errors coming from the index.html imports or the index,js imports? https://vizhub.com/brettforbes/376352ffa3c149829be74cec0d053313

curran commented 3 months ago

I think it probably has to do with the mixing of import styles.

On the one hand, you are trying to use Vanilla JS by importing scripts with the <script> tag. That by itself should work, provided the scripts are actually vanilla JS scripts - without the ES6 imports.

On the other hand, your scripts imported in that way use the ES6 import syntax, like import { select, json, drag, event } from 'd3';, which is not compatible with Vanilla JS <script> tags.

In order to support that kind of import, the concept of index.js and bundle.js was introduced in VizHub v2, which, along with package.json allows you to author code as ES modules. If you want to leverage this way of doing it, you'll need to organize your code such that index.js is the entry point, which imports from all the other local files. That generates bundle.js, which should be the only script you pull into the index.html.

If you don't want to use that bundling feature, and truly want to use the old <script> tag way of importing scripts, then you'll need to get rid of all the ES6 imports that occur there. So, replacing import { select } from 'd3' with d3.select. However, I would not recommend this.

In any case, there's a lot going on in that example. I would suggest to start with something small and get it working, then incrementally build the complexity back up. In summary, there are 3 ways you can organize your scripts:

For your case, it looks like you want to leverage HTML, so perhaps the VizHub v2 runtime might be the best way to go. Good luck!

brettforbes commented 3 months ago

Great explanation, thanks. You have clarified things for me greatly.

I like either the v2 approach best because i can still have some named div's on the html page and i understand it reasonably well. I just need to get a group of radio buttons going as well, and respond to their changes from the index.js page. Hmm, need to work on that

I may have to create the buttons through javascript/d3, will have to research this

Yes there are a lot of pieces going on in there, and to be fair i did produce the tree view based on a single file first. sing a radio button to switch through datasets is stage 2. Stage 3 is the second svg working page.

Basically, this is just developing the two visualisations, which will later on go into a SaaS/UI framework. Just so you can make sense of it, you can imagine the whole page being something like image

The middle pane will be pretty cool, it will look something like the following. I am doing that in webCola using the Bounding box example

image
curran commented 3 months ago

Wow! That looks like a pretty neat project! The pieces all seem doable.

This conversation makes me realize that VizHub needs better documentation, in a prominent place in the platform, that explains all this.

Here's a first stab at it. I'd like to put this in the site at some point:

Creating clear and comprehensive documentation is essential for helping users understand and effectively use VizHub's runtime options. Here's a structured documentation outline based on the provided raw material:

VizHub Runtime Options

Overview

VizHub offers various runtime options to cater to different development styles and requirements. Understanding these options will help you organize your scripts and leverage VizHub's features effectively.

Runtime Options

1. Vanilla JavaScript with <script> Tags

2. VizHub V2 Runtime

3. VizHub V3 Runtime (Hot Vizzes)

Hot Reloading

Interactive Widgets

Exporting and Building

Conclusion

By choosing the appropriate runtime option and leveraging hot reloading and interactive widgets, you can streamline your development process and enhance collaboration in VizHub. Explore the examples provided to get a better understanding of how to implement these features in your projects.

brettforbes commented 3 months ago

Hey that would be so useful.

I am sure for topline JS people your setup seems sensible, yet i am more of a Python person, and the whole JS setup seems pretty weird, so your documents are lifesavers!! Thanks

I'm glad you like the viz prototypes I am building. There are some new concepts there in visual working which look great. Its also interesting how easy it is to slice and dice complex data domains by applying hierarchies to features, and having this focus on defining and attaching sub-graphs, and then finally promoting them as official records. There's some cool features for sure, and I want to build some more novel viz's in the future.

We are building an open-source, low-code, content-driven visualisation platform and are working on our first application in cyber security.

Our architect, @wildone, has proposed a method by which we will be able to develop a visualization in vizhub, and integrate it with an object to which we write to/from for events/data (e.g. mouse-click), and then publish it as a widget on our platform.

That way, others can use your platform to develop visualisations that can convert into widgets in our platform. Working together can only help everyone.

See @wildone for details, obviously I am not the right one for that.

brettforbes commented 3 months ago

help,.

why cant i use webCola using your version 2.0 approach? I does not seem to load properly.

Can you get it to load using import statements? Your approach does not seem to work with this library. What to do? I need to demo on Thursday https://vizhub.com/brettforbes/d1ec7715179542a39bf4bbc2ed99e4e0

curran commented 3 months ago

WebCola is somewhat of a unique case. For that you can try just accessing the global window.cola. The ES6 imports and references to globals both still work in the V2 runtime.

This is a good case that I'd like to support better in the future. WebCola is tricky because the CDN-hosted library does not include a browser build. For that it looks like we need to use that custom CDN URL that pulls the code from GitHub.

brettforbes commented 3 months ago

Following your advice, I removed the import statements and then just used this line

  var cola = window.cola.d3adaptor().size([width, height]);

and it seemed to now import the file without complaining. Of course, I still have errors in it, but it seems pretty solid.

Can you look at private vizhub files?

curran commented 3 months ago

Glad it works!

Can you look at private vizhub files?

Only if you add me as a collaborator on your private viz, I can view it.

image