ipyvuetify icon indicating copy to clipboard operation
ipyvuetify copied to clipboard

Including additional js libraries

Open gioxc88 opened this issue 3 years ago • 8 comments

Is there a way to include additional javascript libraries or custom js modules?

gioxc88 avatar May 21 '21 16:05 gioxc88

I would also very much like to know how to do this.

I am able to define data and methods in the script tag of the vuetify template; I started by implementing the datatable example and then added a script tag above the template to modify the filtering behaviour, like so:.

import ipyvuetify as v
import traitlets

TEMPLATE = '''
<script>
export default {
    'data': {
        ...
    },
    'methods': {
        customFilter(value, search, item) {
            ...
        }
    }
}
</script>
<template>
    <v-data-table
            :headers="headers"
            :items="items"
            :search="search"
    >
</template>
'''

class VuetifyTest(v.VuetifyTemplate):
    template = traitlets.Unicode(TEMPLATE).tag(sync=True)

VuetifyTest()

However, I can't get imports to work, it seems that any lines of javascript above export default { are ignored.

tedwards-flf avatar Jun 03 '21 10:06 tedwards-flf

You could import libraries with requirejs:

export default {
  methods: {
    updatePlot() {
      requirejs(['https://cdn.plot.ly/plotly-1.58.4.min.js'], (Plotly) => {
        Plotly.react(...)
      })
    }
  }
}

But you can't add any javascript outside export default {...} with ipyvuetify-template. Ipyvuetify-template tries to look like Vue as much as possible, but is different in some places.

mariobuikhuizen avatar Jun 03 '21 13:06 mariobuikhuizen

You could import libraries with requirejs:

export default {
  methods: {
    updatePlot() {
      requirejs(['https://cdn.plot.ly/plotly-1.58.4.min.js'], (Plotly) => {
        Plotly.react(...)
      })
    }
  }
}

But you can't add any javascript outside export default {...} with ipyvuetify-template. Ipyvuetify-template tries to look like Vue as much as possible, but is different in some places.

thanks for the answer, that is very useful. Can I ask instead if I were to fork the project if there is a way to change the structure of the package such that any additional js library or file is made available for all the vue templates?

Many thanks

gioxc88 avatar Jun 04 '21 12:06 gioxc88

You could import libraries with requirejs:

export default {
  methods: {
    updatePlot() {
      requirejs(['https://cdn.plot.ly/plotly-1.58.4.min.js'], (Plotly) => {
        Plotly.react(...)
      })
    }
  }
}

But you can't add any javascript outside export default {...} with ipyvuetify-template. Ipyvuetify-template tries to look like Vue as much as possible, but is different in some places.

I couldn't get this to work, it says "requirejs not defined". When I try to follow the instructions from requirejs it says to include a script tag which loads the require.js module, and to put the source code in a separate javascript file (for example, app.js), like so:

<script data-main="js/app.js" src="js/require.js"></script>

but then that doesn't work either.

tedwards-flf avatar Jun 04 '21 19:06 tedwards-flf

Ah, yes, in Jupyter Lab requirejs is not available (in the classic notebook and in Voila it is), you could load it yourself:

...
methods: {
    async updatePlot() {
      await this.loadRequire()
      requirejs(['https://cdn.plot.ly/plotly-1.58.4.min.js'], (Plotly) => {
         console.log('Plotly loaded', Plotly)
         // Plotly.react(...)
      })
    },
    loadRequire() {
      if (window.requirejs) {
          return Promise.resolve()
      }
      return new Promise((resolve, reject) => {
        const script = document.createElement('script');
        script.src = 'https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js';
        script.onload = resolve;
        script.onerror = reject;
        document.head.appendChild(script);
      });
    }
  }

mariobuikhuizen avatar Jun 06 '21 17:06 mariobuikhuizen

Ah, yes, in Jupyter Lab requirejs is not available (in the classic notebook and in Voila it is), you could load it yourself:

...
methods: {
    async updatePlot() {
      await this.loadRequire()
      requirejs(['https://cdn.plot.ly/plotly-1.58.4.min.js'], (Plotly) => {
         console.log('Plotly loaded', Plotly)
         // Plotly.react(...)
      })
    },
    loadRequire() {
      if (window.requirejs) {
          return Promise.resolve()
      }
      return new Promise((resolve, reject) => {
        const script = document.createElement('script');
        script.src = 'https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js';
        script.onload = resolve;
        script.onerror = reject;
        document.head.appendChild(script);
      });
    }
  }

Thanks, trying out your example in classic jupyter notebook or voila works. However, I'd like to download the javascript module and import it locally. When I attempt to do that with the plotly example, I get a 403 (forbidden error). I put the plotly-1.58.4.min.js file in a folder called js/ and do chmod -R 777 js and ensure that I own the file and that all the permissions are set.

Can requirejs work with local files?

tedwards-flf avatar Jun 07 '21 08:06 tedwards-flf

requirejs can, but jupyter-server doesn't serve files from your working directory. I don't know how to get it to serve static files.

mariobuikhuizen avatar Jun 07 '21 11:06 mariobuikhuizen

I see for example lodash is included How can I make additional libraries to be available for import in the script tag?

gioxc88 avatar May 29 '22 04:05 gioxc88