{% macro cdn_jslib(local_filename, cdn_url, libobj, integrity=None) %}
<script src="{{ cdn_url }}" crossorigin="anonymous"
        {% if integrity is not none %}
        integrity="{{ integrity }}"
        {% endif %}
        ></script>
<script>jsFallbackFromCDN('{{ libobj }}', '{{ local_filename }}');</script>
{% endmacro %}

{% macro embed_jslib(local_filename) %}
<!-- INSERTED {{ local_filename }} -->
<script type="text/javascript">{{ offline_file(local_filename) }}</script>
{% endmacro %}

{% macro cdn_stylesheet(local_filename, cdn_url) %}
<link rel="stylesheet" href="{{ cdn_url }}">
<link rel="stylesheet" href="{{ static_ref(local_filename) }}">
{% endmacro %}

{% macro embed_stylesheet(local_filename) %}
<!-- INSERTED {{ local_filename }} -->
<style>{{ offline_file(local_filename) }}</style>
{% endmacro %}

<!DOCTYPE html>
<html>
  <head>
    <title>{{ title }}</title>
    <meta charset="UTF-8" />

    <link rel="icon" type="image/png" sizes="16x16" href="{{ static_ref('images/favicon-16x16.png') }}">
    <link rel="icon" type="image/png" sizes="32x32" href="{{ static_ref('images/favicon-32x32.png') }}">
    <link rel="icon" type="image/png" sizes="96x96" href="{{ static_ref('images/favicon-96x96.png') }}">

    <script>
      function jsFallbackFromCDN(libname_to_check, localFilename) {
          var parts = libname_to_check.split('.')
          if (((parts.length == 1) && (typeof window[parts[0]] == "undefined")) ||
              ((parts.length == 2) && (typeof window[parts[0]][parts[1]] == "undefined"))) {
              console.log(`***Failed to load ${localFilename} from CDN. Falling back to local offline version.***`);
              document.write(unescape(`%3Cscript src="./offline/${localFilename}" type="text/javascript"%3E%3C/script%3E`));
          }
      }
    </script>

    {{ cdn_jslib("jquery-3.6.4.min.js", "https://code.jquery.com/jquery-3.6.4.min.js", "jQuery",
    integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8=") }}

    {{ cdn_jslib("jquery-ui.min.js", "https://code.jquery.com/ui/1.12.1/jquery-ui.min.js", "jQuery.ui",
    integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=") }}

    {{ cdn_jslib("plotly-3.0.1.min.js", "https://cdn.plot.ly/plotly-3.0.1.min.js", "Plotly") }}
    {{ cdn_jslib("katex.min.js", "https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.js", "katex") }}
    {{ embed_jslib("pygsti_plotly_ex.js") }}

    {{ cdn_stylesheet("smoothness-jquery-ui.css", "https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css") }}
    {{ cdn_stylesheet("katex.css", "https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css") }}
    {{ embed_stylesheet("pygsti_dataviz.css") }}
    {{ embed_stylesheet("pygsti_dashboard.css") }}
    {{ embed_stylesheet("pygsti_fonts.css") }}
    
    <script>
      window.plotman = new PlotManager();
    </script>
    
  </head>
  
  <body>

    <div>
      {{ singleItem|render }}
    </div>
    
  </body>
</html>
