Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save the tool outside the jupyter notebook so that the images appear on the display? #239

Open
wesnasimone opened this issue Apr 30, 2021 · 4 comments

Comments

@wesnasimone
Copy link

wesnasimone commented Apr 30, 2021

Hello,

I have doubts if it is possible to save the tool outside the jupyter notebook so that the images appear on the display. I can save in html format, but the images do not appear only the colored circles that are standard of the tool. Is it possible to resolve this for an image database? Below is an excerpt from my code that saves the display:

#Displaythe Dive visualization for this data
from IPython.core.display import display, HTML


sprite_size = 32

# Create Facets template
HTML_TEMPLATE = """<script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/1.3.3/webcomponents-lite.js"></script>
<link rel="import" href="https://raw.githubusercontent.com/PAIR-code/facets/1.0.0/facets-dist/facets-jupyter.html">
        <facets-dive sprite-image-width="{sprite_size}" sprite-image-height="{sprite_size}" id="elem" height="600" atlas-url="sprite_atlas.png"></facets-dive>
        <script>
          document.querySelector("#elem").data = {jsonstr};
        </script>"""

html = HTML_TEMPLATE.format(jsonstr=jsonstr, sprite_size=sprite_size)


# Display the template
display(HTML(html))
#save as html
OUTPUT_PATH = './ISIC_2019_4.html'
with open(OUTPUT_PATH,'w') as fo:
        fo.write(html)

In general, I'm trying to host an instance of Facets Dive outside of a notebook to shared with other people that wanted to work with tool.

I'm annexed the examples of display on jupyter and when it saved in html format

Display on jupyter:

Jupyter notebook

Display save in html format:

save_html

Why don't the images appear?
Has anyone had this problem or could you help me?
Thanks :)

@jameswex
Copy link
Collaborator

jameswex commented May 3, 2021

If you want to host an instance of Facets Dive outside of a notebook, on your own webpage, then you can follow the same approach taken by our facets.dev website, where we set up a webpage and in our case we use GitHub Pages to host it.

https://github.com/PAIR-code/facets/blob/gh-pages/index.html shows our code for the main facets.dev page with its facets dive demo on it.

https://github.com/PAIR-code/facets/blob/gh-pages/quickdraw.js and https://github.com/PAIR-code/facets/blob/gh-pages/quickdraw.html show the code for the https://pair-code.github.io/facets/quickdraw.html demo which includes images.
The datafiles (json raw data, and the sprite image) can be seen in the same https://github.com/PAIR-code/facets/tree/gh-pages directory.

@wesnasimone
Copy link
Author

I'm trying to test these codes, but I'm getting an error.

The code is this:

  • html
<!DOCTYPE html>

<html>
<head>
  <meta charset="utf-8">
  <script src="d3.min.js"></script>
  <script>
    
  window.addEventListener('WebComponentsReady', function() {
    var link = document.createElement('link');
    link.rel = "import";
    link.href = "facets.html";
    link.onload= function() {
      var dive = document.createElement('facets-dive');
      dive.crossOrigin = "anonymous";
      dive.spriteImageWidth = "40";
      dive.spriteImageHeight = "40";

      document.body.appendChild(dive);
      var script = document.createElement('script');
      script.async = true;
      script.src = 'dogs.js';
      document.head.appendChild(script);
    }
    document.head.appendChild(link);
  });
</script>
  <script type="text/javascript" src="dogs.js"></script>
<style>
body, html {
  height: 100%;
  margin: 0;
  padding: 0;
  width: 100%;
}
body {
  display: flex;
  flex-direction: column;
}
.header {
  align-items: center;
  background-color: #ffd139;  /* Quick Draw yellow. */
  display: flex;
  height: 35px;
  justify-content: center;
  width: 100%;
}

.header label {
  font-family: "Comic Sans MS";
  margin: 0 10px;
}
</style>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-46457317-7', 'auto');
  ga('send', 'pageview');
</script>
<title>Quick, Draw! Year-of-the-dog special</title>
</head>
<body>
  <div class="header">
    <label>
      <span><a href="https://quickdraw.withgoogle.com/data">Quick, Draw!</a> testando</span>
    </label>
    <label>
      <span>Preset View:</span>
      <select class="presets"></select>
    </label>
  </div>
</body>
</html>
  • js
var fd = document.querySelector('facets-dive');

var url = ''
d3.json('random-dogs.json', function(data) {
  fd.data = data;
  fd.atlasUrl = 'random-dogs.png';
});

var PRESET_VIEWS = [
  {
    description: '-- select --',
    settings: {
      'verticalFacet': '',
      'verticalBuckets': 10,
      'horizontalFacet': '',
      'horizontalBuckets': 10,
      'positionMode': 'stacked',
      'verticalPosition': '',
      'horizontalPosition': '',
      'colorBy': ''
    },
  },
  {
    description: 'Correctness vs. Number of Points',
    settings: {
      'verticalFacet': 'recognized',
      'verticalBuckets': 3,
      'horizontalFacet': 'numpoints',
      'horizontalBuckets': 25,
      'positionMode': 'stacked',
      'verticalPosition': 'numpoints',
      'horizontalPosition': 'numstrokes',
      'colorBy': ''
    }
  },
  {
    description: 'US vs. GB Recognition',
    settings: {
      'verticalFacet': 'country',
      'verticalBuckets': 2,
      'horizontalFacet': 'recognized',
      'horizontalBuckets': 3,
      'positionMode': 'stacked',
      'verticalPosition': '',
      'horizontalPosition': '',
      'colorBy': ''
    }
  },
  {
    description: 'Strokes vs. Liklihood of Correctness',
    settings: {
      'verticalFacet': 'recognized',
      'verticalBuckets': 3,
      'horizontalFacet': '',
      'horizontalBuckets': 10,
      'positionMode': 'stacked',
      'verticalPosition': '',
      'horizontalPosition': '',
      'colorBy': 'numstrokes'
    }
  },
  {
    description: 'Strokes vs. Points Scatterplot',
    settings: {
      'verticalFacet': '',
      'verticalBuckets': 2,
      'horizontalFacet': '',
      'horizontalBuckets': 10,
      'positionMode': 'scatter',
      'verticalPosition': 'numpoints',
      'horizontalPosition': 'numstrokes',
      'colorBy': 'recognized'
    },
  },
  {
    description: 'Recognition Varies with Timestamp',
    settings: {
      'verticalFacet': '',
      'verticalBuckets': 10,
      'horizontalFacet': 'timestamp',
      'horizontalBuckets': 100,
      'positionMode': 'stacked',
      'verticalPosition': '',
      'horizontalPosition': '',
      'colorBy': 'recognized'
    },
  },
  {
    description: 'Small Scatterplots by Country/Timestamp',
    settings: {
      'verticalFacet': 'timestamp',
      'verticalBuckets': 24,
      'horizontalFacet': 'country',
      'horizontalBuckets': 6,
      'positionMode': 'scatter',
      'verticalPosition': 'numpoints',
      'horizontalPosition': 'numstrokes',
      'colorBy': 'recognized'
    },
  },
];

var presets = document.querySelector(".presets");
d3.select(presets)
    .selectAll('option')
    .data(PRESET_VIEWS)
    .enter()
    .append('option')
    .attr('value', function(view, index) { return index; })
    .text(function(view) {return view.description;});

var loadSettings = function(index) {
  var {settings} = PRESET_VIEWS[index];
  for (var attr in settings) {
    fd[attr] = settings[attr];
  }
};
presets.onchange = function() {
  loadSettings(+presets.value);
};
loadSettings(0);
  • Error
    erro

I'm not understand this error. I put in the code both random-dogs.json and random-dogs.png. What could be going wrong? Thanks

@jameswex
Copy link
Collaborator

It seems like the javascript is being executed before the HTML code to create the facets-dive and other elements have been created.

The HTML code loads the dogs.js inside the WebComponentsReady callback, which should mean it doesn't execute until the elements are built. But you also load dogs.js with a <script> tag explicitly. Try removing the "<script type="text/javascript" src="dogs.js"></script>" line from the html perhaps.

@jameswex
Copy link
Collaborator

Also feel free to remove the below section, as its just for our collecting of site visits on the facets homepage:
`

<script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-46457317-7', 'auto'); ga('send', 'pageview'); </script>

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants