Javascript and self.call_js()

What I’m trying to do:
Adding a Form selecting Custom HTML and calling a JS function with self.call_js()

What I’ve tried and what’s not working:
when i select Edit Custom HTML in the form it never saves the code ( always reverts back to
“Insert your custom HTML here”

Has to be something simple that escapes me …

I had the same issue before as well, just slow update. You need to put the code and wait like 30s then it is saved

I’ve recently adopted the practice of doing everything from code, i.e., not using the Anvil Designer at all (it costs a few more lines of code but ensures that your code “tells the full story”). As part of this endeavour, I associate a form with an HTML template by setting the form’s html attribute (in the form’s init) to the relevant HTML file in Assets (if you do not care about text-highlighting and automatic indentation for your HTML markup you can also treat your HTML markup as a (potentially, multi-line) Python string and assign this string to the form’s html attribute). This practice does not cause any delays.

class Form1(Form1Template):
  def __init__(self, **properties):
    self.init_components(**properties)
    self.html = '@theme:main.html'  # Similar to selecting main.html in Designer.
    # Or: self.html = '<div anvil-slot-repeat="default"></div>'
3 Likes

It saves me a lot of time, so thankful for your post

class Form1(Form1Template):
def init(self, **properties):
self.init_components(**properties)
self.html = ‘@theme:main.html’ # Similar to selecting main.html in Designer.
# Or: self.html = ‘

nice !!! , thanks for this wisdom …

This pratice actually offers a lot more benefits such as reusing UI in another app as well.

Would you mind sharing more details (links, … resource…)?

Sure:
https://anvil.works/build#clone:USLBZZ2XBG72HTEO=AUNX25RBCTBAF43QXHGE5KNV

2 Likes

Really appreciate that, cheers

I like the idea of doing everything in code, I think a powerful toolset/skillset would also be using the designer to basically create live mockups, then coding in place until your designer window was empty.

I could even imagine building a tool (in python of course) to parse the yaml and replace the components programmatically. :exploding_head:

1 Like