{% if site.use_show_widgets_button and page.has_widgets %}
<!-- Include nbinteract for interactive widgets -->
<script src="https://unpkg.com/nbinteract-core" async></script>

<script>
let interact

const initializeNbinteract = () => {
  // If NbInteract hasn't loaded, wait one second and try again
  if (window.NbInteract === undefined) {
    setTimeout(initializeNbinteract, 1000)
    return
  }

  if (interact === undefined) {
    console.log('Initializing nbinteract...')
    interact = new window.NbInteract({
      baseUrl: 'https://mybinder.org',
      spec: '{{ site.binder_repo_org }}/{{ site.binder_repo_name }}/{{ site.binder_repo_branch }}',
      provider: 'gh',
    })
    window.interact = interact
  } else {
    console.log("nbinteract already initialized...")
  }

  interact.prepare()
}

// Initialize nbinteract
initFunction(initializeNbinteract);
</script>
{% endif %}