import * as libembed from './libembed' ; let cdn = 'https://cdn.jsdelivr.net/npm/' ; let onlyCDN = false ; const scripts = document . getElementsByTagName ( 'script' ) ; Array . prototype . forEach . call ( scripts , ( script ) => { cdn = script . getAttribute ( 'data-jupyter-widgets-cdn' ) || cdn ; onlyCDN = onlyCDN || script . hasAttribute ( 'data-jupyter-widgets-cdn-only' ) ; } ) ; param pkg const requirePromise = function ( pkg ) : Promise < any > { return new Promise ( ( resolve , reject ) => { const require = ( window as any ) . requirejs ; if ( require === undefined ) { reject ( 'Requirejs is needed, please ensure it is loaded on the page.' ) ; } else { require ( pkg , resolve , reject ) ; } } ) ; } ; function moduleNameToCDNUrl ( moduleName , moduleVersion ) { let packageName = moduleName ; let fileName = 'index' ; let index = moduleName . indexOf ( '/' ) ; if ( index != - 1 && moduleName [ 0 ] == '@' ) { index = moduleName . indexOf ( '/' , index + 1 ) ; } if ( index != - 1 ) { fileName = moduleName . substr ( index + 1 ) ; packageName = moduleName . substr ( 0 , index ) ; } return `${ cdn }${ packageName }@${ moduleVersion }/dist/${ fileName }` ; } param moduleName param version export function requireLoader ( moduleName , moduleVersion ) { const require = ( window as any ) . requirejs ; if ( require === undefined ) { throw new Error ( 'Requirejs is needed, please ensure it is loaded on the page.' ) ; } function loadFromCDN ( ) { const conf = { paths : { } } ; conf . paths [ moduleName ] = moduleNameToCDNUrl ( moduleName , moduleVersion ) ; require . config ( conf ) ; return requirePromise ( [ `${ moduleName }` ] ) ; } if ( onlyCDN ) { console . log ( `Loading from ${ cdn } for ${ moduleName }@${ moduleVersion }` ) ; return loadFromCDN ( ) ; } return requirePromise ( [ `${ moduleName }` ] ) . catch ( ( err ) => { const failedId = err . requireModules && err . requireModules [ 0 ] ; if ( failedId ) { require . undef ( failedId ) ; console . log ( `Falling back to ${ cdn } for ${ moduleName }@${ moduleVersion }` ) ; return loadFromCDN ( ) ; } } ) ; } param element param loader export function renderWidgets ( element = document . documentElement , loader = requireLoader ) { requirePromise ( [ '@jupyter-widgets/html-manager' ] ) . then ( ( htmlmanager ) => { const managerFactory = ( ) : any => { return new htmlmanager . HTMLManager ( { loader : loader } ) ; } ; libembed . renderWidgets ( managerFactory , element ) ; } ) ; }