d3.csv not finding input file - csv

I've looked for this problem - I see others have the same problem, BUT the fixes don't fix it.
The fix mentioned was to run a web server, which I did.
In my html header I have
<script type="text/javascript" src="d3.v4.js"></script>
I my html body I have
<script type="text/javascript" src="stuff.js"></script>
stuff.js contains:
d3.csv("stuff.csv").get(function(error, data){
console.log(error)
console.log(data);
})
stuff.csv exists. It is in the same directory as a html and js files. It is a good file. I can read it.
One solution that seems to have worked for other people is to use a webserver. So I ran xampp with the document root set to the same directory where the js, html, and csv reside. It still does not work.
In each case, there is a value for error; there is no value for data.

Related

Bokeh Inline Embedding, 'Failed to load resource'

I'm having an issue with embedding Bokeh inline. Particularly, there is an issue with loading the resources from the 'link' tag (refer to html snippet below). For some reason, when I try try to embed a Bokeh plot inline, the following error occurs: 'Failed to load resource: the server responded with a status of 404 (Not Found)', referencing this link - https://cdn.bokeh.org/bokeh/release/bokeh.min.css.map
However, the above address is different from the one I indicate in the link tag (it omits the bokeh version at the end). I have no idea why this error occurs, it's the first time that this happens. I have previously used inline embedding successfully on a number of occasions.
<head>
<link href="https://cdn.bokeh.org/bokeh/release/bokeh-0.12.1.min.css" rel="stylesheet" type="text/css">
<script src="https://cdn.bokeh.org/bokeh/release/bokeh-0.12.1.min.js>
</script>
</head>
EDIT
I am trying to use inline embedding together with jQuery (I would like to display different Bokeh plots without reloading the entire page every time).
When I looked for further error details in the console, I found the following error: "Error rendering Bokeh model: could not find tag with id..."
If it's of any relevance, here is the jQuery script in my html:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
$(function() {
$('a#process_input').bind('click', function() {
$.getJSON('/background_process', {
proglang: $('input[name="proglang"]').val(),
}, function(data) {
$("#result").html(data.a);
$("#r").html(data.b);
});
return false;
});
});
</script>
Where 'data.a' and 'data.b' are the Bokeh-generated script and div tags, respectively.
Any suggestions or advice would be much appreciated!
Best guess is that the script is executing first/early, before the <div> is inserted into the DOM. You will need to find a way to guarantee that the <div> is available by the time the the script executes.
As an aside the partial load use case was not really envisioned when the componenent function was created. If you want to do partial loads, it might be better to serve the doc JSON and then calling Bokeh.embed.embed_items directly on from JavaScript somehow. But it would probably take some experimentation and discussion and back and forth to get that working, which SO is not very good for. I'd encourage you you bring this topic to the public Discourse for further discussion.

Webpack - External JS/JSON file as dependency

Friends,
I'm trying to keep an external file out of the Webpack bundle, but to remain as a dependency - a settings file in this case. I've tried several variations of the following -
externals: {
'Settings': JSON.stringify(require('./settings.json'))
},
...but Webpack keeps including it in the bundle. The only examples I've found in the docs are of common presets like jQuery, nothing is mentioned of local but external files. Help? Thanks!
So after much research, the only way, as of writing this answer, to fetch an external file is to, in a way, ignore Webpack and simply add another script call to the index.html. This is what I ended up doing -
<body>
<div id="whatever-app-id"></div>
<script type="text/javascript" src="settings.js"></script>
<script type="text/javascript" src="app.js" defer></script>
</body>
For good measure, you can also add the webpack externals as mentioned in #gmaliar 's answer with reference to the global object declared in the settings.js file. However that's somewhat redundant as the object is global anyway.
Hope it helped anyone.
How about?
// Outside of webpack context
var fs = require("fs");
var settings = JSON.parse(fs.readFileSync('./settings.json', 'utf8'));
// Within webpack context
externals: {
'Settings': settings
}

Download file from a HTML GET method with Matlab

I've used StackOverflow for long but I've never had to ask because there is a lot of already answered questions.
Now I am stuck in a Matlab problem I cannot solve:
I am working with Google Trends and I need to download a CSV file with Matlab, as the one you can download from the following link:
https://www.google.com/trends/trendsReport?hl=es&q=dji&tz=Etc%2FGMT-2&content=1&export=1
which is easy to get from its page ( https://www.google.es/trends/explore#q=ford )
My problem is:
I can download it with any browser, even Matlab web browser works, however I haven't found a way to automatize that download.
I have tried with urlread() and I get an HTML file instead of a CSV file:
<html><head><title>Redireccionando</title>
<script type="text/javascript" language="javascript">
// Accessing window.external members can cause IE to throw exceptions.
// Any code that acesses window.external members must be try/catch wrapped
/** #preserveTry */
try {
if (top == self) {
if (window.gtbExternal) {
window.gtbExternal.setM();
} else {
window.external.setM();
}
}
}
catch(err) {
}
</script>
<meta http-equiv="refresh" content="0; url='https://www.google.com/trends#q=dji&hl=es&tz=Etc/GMT-2&content=1'"></head>
<body bgcolor="#ffffff" text="#000000" link="#0000cc" vlink="#551a8b" alink="#ff0000"><script type="text/javascript" language="javascript">
location.replace("https://www.google.com/trends#q\x3ddji\x26hl\x3des\x26tz\x3dEtc/GMT-2\x26content\x3d1")
</script></body></html>
I have also tried with urlread2() which I found around here, and also with a downloadUrl() function that looks like it is based on Java, but my Java knowledge is tiny and I have no idea of what that function does or if I can modify it to suit my problem.
I'm sure someone has already solved that problem in Matlab but I have not been able to find a solution on my own by now. I guess that it is something related to the GET method which I do not know how to handle properly.
If you don't mind your code opening up a window in your system browser, you can automate the download by
url = 'https://www.google.com/trends/trendsReport?hl=es&q=dji&tz=Etc%2FGMT-2&content=1&export=1'
web(url, '-browser');
The problem with using urlread (or webread, which is preferred) is that your link doesn't actually point to the CSV file you want to download; it points to a webpage which contains redirection Javascript. That page is what you see above when you run urlread. When you load this in a browser, the Javascript is executed, which redirects to another page and ultimately the CSV file is generated. But urlread and webread will not execute the Javascript. As far as I know, Matlab can't execute Javascript directly, hence you may need to open a browser to execute the Javascript and generate the CSV file.

I want to render static html in Express.js, but still have app.get() methods for logic

This seems like a ridiculous thing to be hung up on, but I would like to display html files from within app.get() methods. It seems like every answer I've come across uses the solution app.use(express.static(__dirname + '/public'));, but then that does not allow for logic from the server.
What I would like to do, is have my .html files be rendered by express, so I can keep some basic logic:
app.get('/', function(res, req) {
if (condition) {
res.render('this.html');
}
else {
res.render('that.html');
}
}
It seems so silly to me that this is only meant for template files, and that I must not be seeing something simple to make this work.
Edit: Using sendFile does not seem to allow the HTML to include external files, javascript / css.
You can use res.sendfile() (res.sendFile() for new version) to render html files.
Update for question in comment:
You can include your css and javascript files in the html file (you might need to specify the paths for your resources correct to get them loaded):
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<script src="myscripts.js"></script>
</head>

MathJax not working if loaded from local js file or if the source code is included

I am testing the MathJax javascript library in a local HTML file. It works and correctly renders math formulas if I include:
script type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
in the HTML file, but it doesn't work if I include instead:
script type="text/javascript" src="/path_to/mathjax.js"
or even if I put the entire javascript source code.
What is the reason? I need to load mathjax.js from local js file into a local HTML file, so the first way doesn't help me.
(EDIT- I see an error: failed to load /extensions/MathZoom.js)
I had to load the entire MathJax distribution (17 Mb zip file), expand it and put the right configuration string in the HTML file. The final form is:
script type='text/javascript' src='/path_to/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML,local/local'
This may occur due to two reasons
1.You may have not extracted the entire zip folder, so first do that.
Still you are getting the error look into second reason.
2.You are not referring to the file correctly.
eg:
<script type ="text/javascript" src="pathto\mathjax.js">
You have typed an incorrect filename. Correct name of file is MathJax.js.Given below is the correct way.
<script type ="text/javascript" src="pathto\MathJax.js">
MathJax.js is the name of the file in the extracted folder(unless modified)
If all of this is not working,then link to the script in this way:
<script type="text/javascript" src="Pathto\MathJax.js?config=TeX-AMS-MML_HTMLorMML">
The reason that your script is not working is you are not including:
'?config=TeX-AMS-MML_HTMLorMML'