What kinds of files can be stored under Assets? Should static assets be stored anywhere else?

In the Anvil editor, under Assets, if I select “Create file” then Anvil asks me “What kind of file would you like to create?”. There are only three options: HTML, CSS or Javascript. Is it ok to store other kinds of files under Assets, or will Anvil not like this? It looks like I can upload other file types, but can’t then edit them.

I would like to have a JSON file as an asset. If that’s really not allowable I could use the minor hack of pretending it’s Javascript, but I’m not sure why it wouldn’t be allowable. I’m also curious just on general principles what is the right thing to do for other basic textual file types.

Or is there a more appropriate way to host a JSON file within the app?

1 Like

You can Search this Forum (highly recommended!), but if memory serves, people have put many other kinds of files there, including images and document files.

The Anvil IDE may not be able to edit such files, but you should be able to edit them outside of Anvil, and upload the edited versions.

Thanks @p.colbert. I did search, but the fact that the create/edit functions seem to be deliberately restricted to HTML/CSS/JS makes me wonder if Anvil considers this bad practice. It would be reassuring to hear from Anvil people about whether there is any reason NOT to do this (even if some people have done it).
Conversely if there is really no reason not to do it then my next question would be why “create file” is restricted only to certain file types and I would have a feature request: allow generic text editing for miscellaneous file types.

Hi @claresloggett,

Assets were designed to hold files that could be easily referenced from HTML, hence why the default options are HTML, JS & CSS. It can also be helpful to upload images to Assets so that you can refer to them from CSS:

/* Apply background image using Image stored in Assets */
.anvil-role-background-image {
  background: url(_/theme/my-uploaded-image.png);
}

What do you intend to use your JSON data for? There are a few places you could store this file and which one is best will depend on your use case.

1 Like

Thanks @bridget !

What I was using the JSON file for was storing a Vega plot specification - it can be quite a long object. I was then loading it into a Vega plot, implemented as a custom component along the lines of Embedded interactive visualisations in Anvil using e.g. Vega Lite? . I attempted to store it in Assets because the Javascript snippet in the custom HTML of the component was reading it in directly. But I found that I couldn’t conveniently store it in Assets as I couldn’t edit it (unless I turned it into Javascript instead of JSON).

However since I wrote this question I’ve shifted things around so that the plot specification object is stored on the Python (client-side/Skulpt) side of things and fed in to the component from there. So right now I’m actually storing it as a Python object. I could still conceivably store it as a JSON file and read it in from Python, and I still wouldn’t be sure where is the best place to store that file I suppose!

Hi @claresloggett,

It sounds like the best place to store your JSON configuration is in a Simple Object Column in Anvil’s Data Tables Service. You can then retrieve your specification using the Data Tables API, and send it to the JS that renders your plot using call_js().