def add_highcharts_shim_to_stream(stream, highcharts_all): """Generates JS to load Highcharts into Jupyter notebook. For human-readable sample output, see unit test.""" highcharts_lower = [m.replace("-", "_") for m in highcharts_all] stream.write(""" <script type='text/javascript'> if (typeof require !== 'undefined') { """) for lib_name, lib_name_lower in zip(highcharts_all[:-1], highcharts_lower[:-1]): stream.write(""" require.undef("highcharts/{lib_name_lower}"); define('highcharts/{lib_name_lower}', function(require, exports, module) {{ {script} }}); """.format(lib_name_lower=lib_name_lower, script=static.JScript(lib_name).content_string)) stream.write(""" require([{require_names}], function({param_names}) {{ Highcharts = {highcharts}; """.format(require_names=", ".join("'highcharts/{}'".format(l) for l in highcharts_lower[:-1]), param_names=", ".join(highcharts_lower[:-1]), highcharts=highcharts_lower[0])) stream.write("".join("{}(Highcharts);\n".format(l.replace("-", "_")) for l in highcharts_all[1:-1])) stream.write(static.JScript(highcharts_all[-1]).content_string) stream.write("\n") stream.writelines("\n".join( ["window.Highcharts = Highcharts;", "});", "}", "</script>"]))
def test_jscript_write_string_compressed(): script = 'test script' jscript = ps.JScript(script_string=script, name='test name') stream = StringIO() jscript.write_compressed(stream, script) output = stream.getvalue() assert output.startswith('blocksEval') assert script not in output # Do not compress if disabled globally ps.JScript.global_encode = False jscript = ps.JScript(script_string=script, name='test name', encode=False) stream = StringIO() jscript.write_compressed(stream, script) output = stream.getvalue() assert output == script
def test_jscript_write_string(): script = 'test script' jscript = ps.JScript(script_string=script, name='test name', encode=False) output = jscript.write() output_string = output.__str__() assert output_string.startswith('<script type="text/javascript">') assert output_string.endswith('</script>') assert script in output_string # Check that output is compressed if we ask for it jscript = ps.JScript(script_string=script, name='test name', encode=True) output = jscript.write() output_string = output.__str__() assert output_string.startswith('<script type="text/javascript">') assert output_string.endswith('</script>') assert 'RawDeflate' in output_string assert script not in output_string
def test_jscript_raises_with_no_name_and_string(): with pytest.raises(ValueError): ps.JScript()