Is there any way to read a file line by line in javascript, specifically this file which is a dictionary. I was trying to build a replica of a java anagram solver I made a few months ago, but hit this problem of not being able to read a file line by line. I could download the file and store it locally if that would make any difference to being able to read it.
Use YQL:
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fdocs.oracle.com%2Fjavase%2Ftutorial%2Fcollections%2Finterfaces%2Fexamples%2Fdictionary.txt%22&format=json&diagnostics=true&callback=cbfunc
Here's what the fiddle looks like:
window.callback = function(a) { window.file = a.query.results.body.p; go(); };
$.getScript('http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fdocs.oracle.com%2Fjavase%2Ftutorial%2Fcollections%2Finterfaces%2Fexamples%2Fdictionary.txt%22&format=json&diagnostics=true&callback=callback');
window.go = function() {
var terms = file.split(' ');
for (var i = 0; i < 100; i++)
console.log(terms[i])
};
The fiddle only does the first 100 but you get the idea (I hope).
In most circumstances, you could just read the file into memory and then parse it into lines. If you read the whole thing into memory with an ajax call, you could just use data.split("\n") to convert it to an array of lines.
You should initiate Ajax Request for such Operation. Reading a file line by line although is not recommended via Ajax, as you will end up create loads of server requests in the process;as JavaScript is all about Client side and limited access also. Ajax request to server is recent years add-on to it.
Definitely you are searching some information using some keywords; so it would be wise if you add the search logic on server functions; call the specific function via Ajax and return back result-set to the browser. That function could be a file that generates result or, a web service. You choose your flavor.
Alternate option would be to re-code the file information to JSON (about JSON) at start and let it roll to client js script. I would not recommend XML for this as its gonna eatup loads of browser memory in the processing terms. Been there! :(. Since JavaScript has native support to JSON, it will go smooth. Warning this exposes data to local.
Related
I have a question that has been asked several times here and other places around the internet*, but answers I've seen are incomplete or ineffective.
I would like to have a JavaScript function runPy() that, upon being called (in a browser, via a button-click for instance), will execute a Python script in my server that we'll call test.py.
Let's say that test.py is simply designed to create a text file and write in it 'hello world'
Python
f = open('test.txt', 'w+')
f.write('hello world')
Based on other answers, I have pieced together the following JavaScript/jQuery function:
JavaScript
function runPy() {
$.ajax({
type:'POST',
url:'test.py',
success: function(data) {
console.log(data)
};
});
}
Now, this is of course incorrect. As one might expect, rather than running the Python script, it prints to the console the contents of the script:
Console
f = open('test.txt', 'w+')
f.write('hello world')
How would I go about editing my JavaScript (and/or Python) to achieve the functionality I would like? In a perfect world, I would like to avoid importing any new dependencies (I've seen some answers dependent on Flask or Django) but of course beggars can't be choosers.
Additionally, if I will allow myself to get greedy, it would be very nice to be able to pass arguments to the Python script as well, or even use JavaScript to call a specific function in the Python script, and have the results passed back to the client-side JavaScript.
*Similar Questions
Call python function from JS
how to call python script from javascript?
Run Python scripts from JavaScript functions
You're going on the right path.
But, here's why the POST isn't working. Except for HTML filetype, making a POST call to fetch a static file on the server (i.e. random.js, random.css etc) will print the raw file content.
In this scenario, you'll need to handle this on the server-side backend code. I don't know which backend framework you're using but here are some articles that can help you with this:
NodeJS: https://medium.com/swlh/run-python-script-from-node-js-and-send-data-to-browser-15677fcf199f
.NET: https://medium.com/better-programming/running-python-script-from-c-and-working-with-the-results-843e68d230e5
Java: Running a .py file from Java
UPDATE!!: Due to the recent developments in the space of Web Development, it is now possible to run Python on the Client-side through WebAssembly. Please find more instructions here: Pyodide
I have a JavaScript file on my server that contains a function. I would like to develop a REST Api to connect to this server, run the JavaScript function and send back the output.
Is it possible to call a JavaScript function from a php file?
I read this but it doesn't answer my question, because my js file is hosted on the same server as the php file.
Is the V8Js extensions what I am looking for?
Edit
The js function looks like this:
function (line, userWeight, weightunit){
//logic is here
var computed = {
userLengthFtin: userLengthFtin,
userLevel: userLevel,
proId: line['id'],
proLengthFeetin: proLengthFeetin,
proThick: proThickFtin,
weightunit: weightunit
};
return computed;
}
Is it possible to call a javascript function from a php file ?
You would need to hand things over to some other software which can execute JS. This might be through shelling out or it might be though a library such as Selenium or the V8js library you found.
Whatever you choose, it would need to be able to handle the particular needs of the JS (e.g. if the JS expects to be embedded in a webpage with access to a DOM and all the APIs provided by a web browser, then you couldn't simply run it with Node.js).
It would probably be simpler to rewrite the function in PHP.
I would like to retrieve the contents of a javascript script instead of executing it upon requesting it.
EDIT: I understand that Python is not executing the javascript code. The issue is that when I request this online JS script it gets executed. I'm unable to retrieve the contents of the script. Maybe what I want is to decode the script like so http://jsunpack.jeek.org/dec/go
That's what my code looks like to request the js file:
def request(self, uri):
data = None
req = urllib2.Request(uri, data, self.header)
response = urllib2.urlopen(req)
html_text = response.read()
return html_text.decode()
I know approximately what the insides of the script look like but all I get after the request is issued is a 'loaded' message. My guess is that the JS code gets executed. Is there any way to just request the code?
There is no HTML or JavaScript interpreter in urllib2. This module does nothing but fetch the resource and return it to you raw; it certainly will not attempt to execute any JavaScript code it receives. If you are not receiving the response you expect, check the URL with a tool like wget or monitor the network connection with Wireshark or Fiddler to see what the server is actually returning.
(decode() here only converts the bytes of the HTTP response body to Unicode characters—using the default character encoding, which probably isn't a good idea.)
ETA:
I guess what I want is to decode the Javascript like so jsunpack.jeek.org/dec/go
Ah, well that's a different game entirely. You can get the source for that here, though you'll also need to install SpiderMonkey, the JavaScript engine from Mozilla, to allow it to run the downloaded JavaScript.
There's no way to automatically ‘unpack’ obfuscated JavaScript without running it, since the packing code can do anything at all and JS is a Turing-complete language. All this tool does is run it with some wrapper code for functions like eval which packers/obfuscators typically use. Unfortunately, this sabotage is easily detectable, so if it's malware you're trying to unpack you'll find this fails as often as it succeeds.
I'm not sure I understand. If I do a simplified version of your code and run it on a URI that's sure to have some javascript:
>>> import urllib2
>>> res = urllib2.urlopen("http://stackoverflow.com/questions/6946867/how-to-unpack-javascript-in-python")
And you print res (or res.decode()), the javascript is intact.
Doing urlopen should retrieve whatever character stream the source provides. It's up to you to do something with it (render it as html, interpret it as javascript, etc).
Almost 6 months ago, I asked a question on stackoverflow "Software to help in log analysis?"
Please look at that question before reading ahead.
It turned out that there is no good software available currently that can intermix log files based on timestamps and present them in a good UI.
I wanted to take initiative and develop something and open source it once its completed.
Earlier, I worked around by writing a quick and dirty piece of code in c++, that would generate a tab separated file (like csv but tab separated) which I would later opened in Excel.
I am not satisfied with my c++ code for the following reasons:
1. It totally depends on Excel to view the output file later.
2. Since there is no UI involved, its not easy to write its commandline everytime.
3. Because of the learning curve of the commandline, its not so much sharable with other team members (and the world).
For the above reasons (and a few more), I was thinking to develop that as a web solution. That way I can share the working instance with everyone.
What I have in mind is a web based solution something like this:
The user will be able to give the input log files using HTML5's File API.
And then user would probably tell the format of the timestamp associated with each log file.
Thereafter, the javascript would process those log files into intermixed HTML output in a table.
I am just a beginner in web based technologies. So I need your help in determining if this would be the best way to go about it?
I want a web solution, but that doesn't mean I want user to upload his log files for backend processing. I want a web-based client only solution.
Thanks for your inputs.
EDIT: Based on comment below by Raynos
#bits You do realise that browsers
were not meant to handle large pieces
of data. There was
stackoverflow.com/questions/4833480/…
which shows that this can cause
problems.
I feel that doing this in browsers isn't the best deal. Probably, I should explore backend based solutions.
Any ideas or suggestions?
Your looking for an online diff tool which takes n files containing a list of timestamps in some order including a extra data to be displayed in place but not parsed in the diffing.
The file upload would involve
<input id="upload" type="file">
Along with snippets of javascript
$("#upload").change(function(files) {
var files = this.files;
for (var i = 0; i < files.length; i++) {
(function() {
var file = files[i];
var reader = new FileReader;
reader.onload = function(e) {
var text = reader.result;
console.log(text);
};
reader.readAsText(file);
}());
}
});
See live example.
So you have all the text you just need to work on a parser. I hope that helps a bit.
As for the markup of the diff I would suggest something like:
<table>
<!-- one tr per unique timestamp -->
<tr>
<!-- one td/textarea per file -->
<td> <textarea /> </td>
<td> <textarea /> </td>
</tr>
...
</table>
I would recommend making this a template and using a template engine to do some of the heavy lifting.
Let's say we want to use jquery-tmpl.
Here's an example to get you started. (I spend zero time on making it look good. That's your job).
All that's left is generating JSON data to insert into the template.
So given your file input you should have an array of fileTexts somewhere.
We want to have some kind of deliminator to split it up into individual time stamp records. For simplicities sake let's say that the new line character would work.
var fileTexts = [file];
var regex = new RegExp("(timestampformat)(.*)");
for (var i = 0; i < fileTexts.length; i++) {
var text = fileTexts[i];
var records = text.split("\n");
for (var j = 0; j < records.length; j++) {
var match = regex.exec(records[j]);
addToTimestamps(match[1], match[2], i);
}
}
function addToTimestamps(timestamp, text, currFileCount) {
console.log(arguments);
// implement it.
}
As per example.
These are the basic building blocks. Get the data from the File API. Manipulate the data into a normalised data format then use some kind of rendering tool on the data format.
This would be fairly easy to do using javascript. You mentioned above using the html5 file api, and that is a great place to start (html file api), you can use unobtrusive javascript to have a callback fire when the a file is uploaded. Inside the callback you could use any of the great javascript templating libraries to construct a table of elements from the uploaded file. Then on subsequent file uploads, you could dynamically interleave them into the table using their timestamp. Detecting the timestamp using js regular expressions would be fairly straightforward, and reasonably efficient if you used a compiled form.
This is a fairly high level answer to the question, and if you have any questions about particular details, I'd be happy to answer those as well.
Hope this helps
if im loading data for the markers from a database do i write the output queried from the db into a javascript file or is there a cleaner way of doing it?
thanks
Yeah, writing to a file is a good way to do it. Just write the data as JSON. Your file would look like:
var map = {waypoints:[...]};
And then you can do:
for(var i=o; i<map.waypoints.length; ++i) {
addWaypoint(map.waypoints[i]);
}
I actually do some static caching of nodes using this method: http://www.trailbehind.com/site_media/javascript/gen/national-parks.js
We use that set of National Parks a lot, so we cache it. But we also have urls where you can fetch JSON for a node on the fly, such as: http://www.trailbehind.com/map/node/7538973/632/735/
This URL gets the map for node 7538973, and specifies the dimensions of their map in pixels as well.
The needed Javascript can of course be wrapped in whatever language you prefer to use, see e.g. pymaps for a Python example. While pymaps is actualally inserting the JS code into an HTML template, if you're writing a web app you can perfectly well choose to serve that JS code on the fly at an appropriate URL and use that URL in a <script> tag in your pages.
Depending on the size of your application, you may want to consider printing out plain javascript.
I have a map that uses server-side clustering, so markers update frequently. I found that parsing JSON markers slowed the app significantly, and simply wasn't necessary.
If speed is an issue, I'd suggesting removing all of the unnecessary layers possible (JSON, AJAX, etc.). If it's not, you'll be just fine with JSON, which is cleaner.
I agree with Andrew's answer (+1).
I guess the only point I would add is that rather than including some server side generated JavaScript, you could use an AJAX request to grab that data. Something like:
var request = new Request.JSON (url: 'get_some_json.php',
onSuccess: function(data) {
// do stuff with the data
}).get ();
(This is a Mootools AJAX thing, but you could use any kind of AJAX request object).
Edit: ChrisB makes a good point about the performance of parsing JSON responses and re-reading my answer I certainly didn't make myself clear. I think AJAX requests are suitable for re-requesting data based on parameters generated by user interaction. I guess an example use case might be, a user filtering the data displayed on the map. You might grab the filtered data via an AJAX/SJON request rather than re-loading the page.