Replacing code with link to a file in ghost blog hbs - handlebars.js

I am using the file_html function in bokeh to produce html that I want to embed into a ghost blog website.
If I load the id in a html card on the site, and then paste the head and body of the file into a code injection/header window, the output is fine.
But the html files can be long. So, rather than pasting the code, I want to save the file in my theme, and then include a link to that file. I've tried different ways, but can't get the chart to show.
This is what pasting the head/body into the code injection header looks like:
script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-2.4.3.min.js"></script>
<script type="text/javascript">
Bokeh.set_log_level("info");
</script>
<script type="application/json" id="1653">
{"1f335636-847c-44ca-ac3d-a39e52279a15":{"defs":[],"roots":{"references":[{"attributes":{"line_alpha":0.2,"line_color":"blue","x":{"field":"Date"},"y":{"field":"A"}},"id":"1474","type":"Line"},{"attributes":{"coordinates":null,"formatter":{"id":"1483"},"group":null,"major_label_policy":{"id":"1484"},"ticker":{"id":"1449"}},"id":"1448","type":"LinearAxis"},{"attributes":{},"id":"1483","type":"BasicTickFormatter"},{"attributes":{"coordinates":null,"formatter":{"id":"1480"},"group":null,"major_label_policy":{"id":"1481"},"ticker":{"id":"1453"}},"id":"1452","type":"LinearAxis"},{"attributes":{"data":{"A":{"__ndarray__":"q2jSD7sDvr+pDX3hPMntvxSFJEWY1eg/MVje9zOj8L+47dbtoFzTPw4XszUKu+0/","dtype":"float64","order":"little","shape":[6]},"B":{"__ndarray__":"1i8GC4Oi8D+uLg3FLEbsvwTnKftsKAPArl2CHJBq5L9l8duvR2/hPzi+J1DhqPA/","dtype":"float64","order":"little","shape":[6]},"C":{"__ndarray__":"hrXr9J6Xdr+t0s/xTnjKvxrjdAWjo/k/9U9XIzQq5D9rBurlogrxP0qoktgjAPg/","dtype":"float64","order":"little","shape":[6]},"D":{"__ndarray__":"s0qFVyjEtL+e/bFQtfTdv46B45hT4Mq/ftVSa9MK7b+DcTodgFj5P8Bs4A3NS/e/","dtype":"float64","order":"little","shape":[6]},"Date":{"__ndarray__":"AAAA57Nrd0IAAMBMBmx3QgAAgLJYbHdCAABAGKtsd0IAAAB+/Wx3QgAAwONPbXdC","dtype":"float64","order":"little","shape":[6]}},"selected":{"id":"1486"},"selection_policy":{"id":"1485"}},"id":"1437","type":"ColumnDataSource"},{"attributes":{},"id":"1440","type":"DataRange1d"},{"attributes":{"line_alpha":0.1,"line_color":"blue","x":{"field":"Date"},"y":{"field":"A"}},"id":"1473","type":"Line"},{"attributes":{},"id":"1484","type":"AllLabels"},{"attributes":{"coordinates":null,"data_source":{"id":"1437"},"glyph":{"id":"1491"},"group":null,"hover_glyph":null,"muted_glyph":{"id":"1493"},"nonselection_glyph":{"id":"1492"},"view":{"id":"1495"}},"id":"1494","type":"GlyphRenderer"},{"attributes":{},"id":"1459","type":"SaveTool"},{"attributes":{},"id":"1442","type":"DataRange1d"},{"attributes":{"line_color":"blue","x":{"field":"Date"},"y":{"field":"A"}},"id":"1472","type":"Line"},{"attributes":{},"id":"1453","type":"BasicTicker"},{"attributes":{},"id":"1457","type":"WheelZoomTool"},{"attributes":{"below":[{"id":"1448"}],"center":[{"id":"1451"},{"id":"1455"},{"id":"1488"}],"left":[{"id":"1452"}],"renderers":[{"id":"1475"},{"id":"1494"}],"title":{"id":"1470"},"toolbar":{"id":"1463"},"x_range":{"id":"1440"},"x_scale":{"id":"1444"},"y_range":{"id":"1442"},"y_scale":{"id":"1446"}},"id":"1439","subtype":"Figure","type":"Plot"},{"attributes":{"axis":{"id":"1452"},"coordinates":null,"dimension":1,"group":null,"ticker":null},"id":"1455","type":"Grid"},{"attributes":{"overlay":{"id":"1462"}},"id":"1458","type":"BoxZoomTool"},{"attributes":{"tools":[{"id":"1456"},{"id":"1457"},{"id":"1458"},{"id":"1459"},{"id":"1460"},{"id":"1461"},{"id":"1508"}]},"id":"1463","type":"Toolbar"},{"attributes":{},"id":"1485","type":"UnionRenderers"},{"attributes":{"source":{"id":"1437"}},"id":"1476","type":"CDSView"},{"attributes":{"label":{"value":"A"},"renderers":[{"id":"1475"}]},"id":"1489","type":"LegendItem"},{"attributes":{},"id":"1486","type":"Selection"},{"attributes":{"bottom_units":"screen","coordinates":null,"fill_alpha":0.5,"fill_color":"lightgrey","group":null,"left_units":"screen","level":"overlay","line_alpha":1.0,"line_color":"black","line_dash":[4,4],"line_width":2,"right_units":"screen","syncable":false,"top_units":"screen"},"id":"1462","type":"BoxAnnotation"},{"attributes":{},"id":"1456","type":"PanTool"},{"attributes":{},"id":"1446","type":"LinearScale"},{"attributes":{"coordinates":null,"group":null,"text":"Trial"},"id":"1470","type":"Title"},{"attributes":{},"id":"1480","type":"BasicTickFormatter"},{"attributes":{"coordinates":null,"group":null,"items":[{"id":"1489"},{"id":"1507"}]},"id":"1488","type":"Legend"},{"attributes":{},"id":"1444","type":"LinearScale"},{"attributes":{"line_alpha":0.2,"line_color":"green","x":{"field":"Date"},"y":{"field":"B"}},"id":"1493","type":"Line"},{"attributes":{},"id":"1460","type":"ResetTool"},{"attributes":{"callback":null,"formatters":{"#Date":"datetime"},"tooltips":[["Date","#Date{%F}"],["A","#{A}{0,0.0}"],["B","#{B}{0,0.0}"]]},"id":"1508","type":"HoverTool"},{"attributes":{},"id":"1461","type":"HelpTool"},{"attributes":{"line_alpha":0.1,"line_color":"green","x":{"field":"Date"},"y":{"field":"B"}},"id":"1492","type":"Line"},{"attributes":{},"id":"1449","type":"BasicTicker"},{"attributes":{"line_color":"green","x":{"field":"Date"},"y":{"field":"B"}},"id":"1491","type":"Line"},{"attributes":{"axis":{"id":"1448"},"coordinates":null,"group":null,"ticker":null},"id":"1451","type":"Grid"},{"attributes":{"source":{"id":"1437"}},"id":"1495","type":"CDSView"},{"attributes":{"label":{"value":"B"},"renderers":[{"id":"1494"}]},"id":"1507","type":"LegendItem"},{"attributes":{},"id":"1481","type":"AllLabels"},{"attributes":{"coordinates":null,"data_source":{"id":"1437"},"glyph":{"id":"1472"},"group":null,"hover_glyph":null,"muted_glyph":{"id":"1474"},"nonselection_glyph":{"id":"1473"},"view":{"id":"1476"}},"id":"1475","type":"GlyphRenderer"}],"root_ids":["1439"]},"title":"Bokeh Application","version":"2.4.3"}}
</script>
<script type="text/javascript">
(function() {
const fn = function() {
Bokeh.safely(function() {
(function(root) {
function embed_document(root) {
const docs_json = document.getElementById('1653').textContent;
const render_items = [{"docid":"1f335636-847c-44ca-ac3d-a39e52279a15","root_ids":["1439"],"roots":{"1439":"4484be71-5c42-4872-ad08-d14d68f5b8be"}}];
root.Bokeh.embed.embed_items(docs_json, render_items);
}
if (root.Bokeh !== undefined) {
embed_document(root);
} else {
let attempts = 0;
const timer = setInterval(function(root) {
if (root.Bokeh !== undefined) {
clearInterval(timer);
embed_document(root);
} else {
attempts++;
if (attempts > 100) {
clearInterval(timer);
console.log("Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing");
}
}
}, 10, root)
}
})(window);
});
};
if (document.readyState != "loading") fn();
else document.addEventListener("DOMContentLoaded", fn);
})();
</script>
And this is my latest attempt to replace the body with a link to the file:
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-2.4.3.min.js"></script>
<script type="text/javascript">
Bokeh.set_log_level("info");
</script>
<script type="text/html"
src="/assets/bokeh_new/s3_partial.html"
></script>

Related

Handlebars display AJAX JSON response

I am new to handlebars.js and I am trying to display a JSON response with this API https://yts.am/api/v2/list_movies.json
I try to use static and basic JSON data, it is working but when I try to load huge amount I get error
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1 id="title"></h1>
<hr>
<div id="result"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script>
<script id="text-template-yts" type="text/x-handlebars-template">
<h2>{{title}}</h2>
<h3>{{summary}}</h3>
<ul>
{{#each torrents}}
<li>{{quality}}</li>
{{/each}}
</ul>
</script>
<script>
function ajax_get(url, callback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log('responseText:' + xmlhttp.responseText);
try {
var data = JSON.parse(xmlhttp.responseText);
} catch (err) {
console.log(err.message + " in " + xmlhttp.responseText);
return;
}
callback(data);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
ajax_get('https://yts.am/api/v2/list_movies.json', function (data) {
document.getElementById("title").innerHTML = data["status_message"];
var source = document.getElementById('text-template-yts').innerHTML;
var template = Handlebars.compile(source);
var html = template(data.data.movies);
document.getElementById("result").innerHTML = html;
});
</script>
</body>
</html>
I was able to get the query success but in parsing is error
FIXED
I need to add the each of handlebars

how to add section Scripts to asp.net mvc Razor View without layout page

My mvc view layout is null. how can i add script inside view.
#{
Layout = null;
}
#section Scripts{
<script type="text/javascript">
$(document).ready(function () {
$('#txtChild1').on('change', function () {
$("#C1Age1").show();
});
});
</script>
}
Error show
cannot resolve section 'Scripts'
If you use Layout = null then you don't need to use #section Scripts any more. you can simply use script tag.
Example:
#{
Layout = null;
}
<script type="text/javascript">
$(document).ready(function () {
$('#txtChild1').on('change', function () {
$("#C1Age1").show();
});
});
</script>
If you are not using layout page then you need not to add section. add scripts as you add them on a html page.
#{
Layout = null;
}
<script type="text/javascript">
$(document).ready(function () {
$('#txtChild1').on('change', function () {
$("#C1Age1").show();
});
});
</script>
You can also add script block without section if it is not mandatory.

TypeError: 'undefined' is not an object (evaluating 'text.replace') - when testing backbone view

I am trying to write the tests for a Backbone app which uses twig and symphony to render the underscore templates, using mocha, chai and sinon, with blanket for code-coverage and phantomjs for browser simulation. I have managed to load the template markup into test.html by doing the following in the body tag:
<!-- fixtures -->
<script>
$(function(){
$("#fixtures").load("fixtures/comment-fixture.html");
});
</script>
I have created a html fixture, or at least that is what I was aiming for, however I still get the following error when running my tests:
TypeError: 'undefined' is not an object (evaluating 'text.replace')
I understand that it refers to basically underscore evaluating _template(undefined), but I don't understand why it is doing that. Has anyone come across the a similar issue before or know why it is triggering this error?
This is my set-up:
Backbone view I want to test:
var CommentView = Backbone.View.extend({
//... is a list tag.
tagName: "li",
model: null,
// Cache the template function for a single item.
template: _.template($('#comment-template').html()),
// This is the timeout used to re-render the times
refreshTimer: null,
initialize: function(options) {
this.model = options.model;
this.listenTo(this.model, 'change', this.render);
this.listenTo(Backbone, 'stopCommentRefresh', this.stopRefreshTimer);
},
render: function() {
this.$el.html(this.template(this.model.renderAttributes()));
this.refreshTimer = setTimeout(_.bind(this.render, this), 60000);
return this;
},
// If you hit `enter`, we're through editing the item.
updateOnEnter: function(e) {
if (e.keyCode == 13) this.close();
},
stopRefreshTimer: function () {
clearTimeout(this.refreshTimer);
this.remove();
}
});
My fixture file for it :
<script type="text/template" id="comment-template">
<article class="comment">
<h4><%- user.username %></h4>
<p><%- comment %></p>
<time><%- time %></time>
</article>
</script>
My test file for it (so far nothing special):
describe("Comment View", function () {
before(function () {
// create test fixture
this.$fixture = $('<li id="comment-view-fixture"></li>');
});
beforeEach(function () {
// empty out and rebind the fixture for each run
this.$fixture.empty().appendTo($("#fixtures"));
// new default model and view for each test
this.view = new CommentView ({
el: this.$fixture,
model: new Comment({
comment: "Hello world!",
created: new Date(),
modified: new Date(),
user: {
username: "Mario"
}
})
});
});
afterEach(function () {
this.view.model.destroy();
});
after(function () {
$("#fixtures").empty();
});
});
And my test.html file:
<html>
<head>
<title>Backbone.js Tests</title>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="js/lib/mocha.css" />
</head>
<body>
<div id="blanket-main" class="hidden" style="display: none;"></div>
<div id="mocha"></div>
<!-- Utility libraries -->
<script src="js/lib/mocha.js"></script>
<script src="js/lib/chai.js"></script >
<script src="js/lib/sinon-chai.js"></script >
<script src="js/lib/sinon.js"></script >
<script src="js/lib/chai-datetime.js"></script>
<!-- JavaScript Coverage Libraries. -->
<script src="js/lib/blanket.js"></script>
<!-- jquery library -->
<script src="../../../../ApiBundle/Resources/public/js/thirdParty/jquery-1.11.js"></script>
<!-- fixtures -->
<script>
$(function(){
$("#fixtures").load("fixtures/comment-fixture.html");
});
</script>
<!-- JavaScript Core Libraries -->
<script src="../../../../ApiBundle/Resources/public/js/thirdParty/underscore.js"></script>
<script src="../../../../ApiBundle/Resources/public/js/thirdParty/jquery.dataTables.js"></script>
<script src="../../../../ApiBundle/Resources/public/js/thirdParty/backbone.js"></script>
<!-- Javascript Application Libraries - Views -->
<script src="../../../../ApiBundle/Resources/public/js/comment.js" data-cover></script>
<script>
var expect = chai.expect;
mocha.setup({
ui: "bdd",
globals: ['stats', 'failures', 'runner'], // Blanket leaks.
bail: false
});
// Set up Mocha with custom Blanket.js reporter.
mocha.reporter(function (_reporter) {
// Updated for Mocha 1.15.1 integration.
// See: https://github.com/alex-seville/blanket/pull/356
var blanketReporter = function (runner) {
// Listeners.
runner.on("start", function () { blanket.setupCoverage(); });
runner.on("suite", function () { blanket.onModuleStart(); });
runner.on("test", function () { blanket.onTestStart(); });
runner.on("test end", function (test) {
blanket.onTestDone(test.parent.tests.length,
test.state === 'passed');
});
runner.on("end", function () {
blanket.onTestsDone();
$("#blanket-main").removeClass("hidden").show("fast");
$("html, body").animate({ scrollTop: 0 });
});
_reporter.call(this, runner);
};
blanketReporter.prototype = _reporter.prototype;
return blanketReporter;
}(mocha._reporter));
blanket.beforeStartTestRunner({
callback: function () {
(window.mochaPhantomJS || mocha).run();
}
});
</script>
<script src="js/spec/views/view_CommentView.spec.js"></script>
<!-- Coverage style helpers -->
<style type="text/css">
#blanket-main {
margin-top: 65px;
margin-right: 20px;
margin-left: 20px;
border-radius: 5px;
border: 1px solid #666;
}
</style>
<!-- Test Fixtures. -->
<div id="fixtures" style="display: none; visibility: hidden;"></div>
</body>
</html>
It turns out its because $("#fixtures").load("fixtures/comment-fixture.html") is asynchronous and isn't actually loaded when I run the tests. By simply copying the contents of fixture.html into test.html body tag, the error stops happening!

image upload and preview in asp.net

I want to browse image from client computer as soon as image is uploaded, it will display preview and when client(I) saves details, it will be saved in the database.
as well as that image will be displayed in page
I have fileuploader
<style type="text/css">
#newPreview
{
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);
}
</style>
<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css "
rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js "></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js "></script>
<script language="javascript" type="text/javascript">
function PreviewImg(imgFile) {
if (navigator.appName != "Netscape") {
var newPreview = document.getElementById("newPreview");
newPreview.style.display = "";
newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value;
newPreview.style.width = "250px";
newPreview.style.height = "200px";
}
else {
if (imgFile.files && imgFile.files[0]) {
var reader = new FileReader();
$('[id*=divFirfox]').css("display", "block");
reader.onload = function (e) {
$('#blah')
.attr('src', e.target.result)
.width(250)
.height(200);
};
reader.readAsDataURL(imgFile.files[0]);
}
}
}
</script>
now what should i do to retrive image from database?
something like link

ASP.NET <Body onload="initialize()">

I am creating an ASP.NET custom control.
I want to set the body onload event of the ASPX page where the control resides.
Please note I cannot rely on the body tag in the ASPX page having runat="server".
any ideas??
Cheers.
Inline javascript! Just incase you can't use jQuery
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function')
{
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
} } }
addLoadEvent(initialize);
link to read http://www.webreference.com/programming/javascript/onloads/
Credits goto http://simonwillison.net/
If you include jQuery in your project you can use this:
jQuery(document).ready(function ()
{
// page load code here
});
You can run these multiple times on a page, ie handy within a control
Try this,
<script type="text/javascript">
window.onload = initialize();
function initialize() {
//your stuff
}
</script>

Resources