Python at backend JS at frontend integration - javascript

Say I have an image blending program written in Python. I want to show the image blending process in a real-time fashion in the frontend while user adjusting the parameters (say several scroll bars), which might require Javascript.
I guess one way to do so is to use libraries mentioned in this similar question: first write a Python class capable of doing the backend job, compile it into JS code and call it from the frontend. Any better ways?
The image manipulation task might be heavier in the future so I write in Python instead of in JS directly.

Depending on the ops you are going to implement, perhaps you could write a parallel implementation of your algorithms using HTML5 Canvas. Then show that in a minimal resolution or provide some way to set up a viewfinder (basically a cropped part of the whole). Once the actual job's done, show the full result.
You might also want to consider using Node.js for something like this. Essentially this would allow you to use pretty much the same code for both backend and frontend reducing duplication of the algorithms.
You could also try to rethink the way you perform the manipulation. Aviary encourages the users to perform one operation at a time. In addition they provide undo (easy to implement). This kind of scheme would work really well with Canvas.

Related

Javascript Statistics Library with Certain Tests

I have several data "layers" over the same list of "samples". Some layers are continuous float data, some are multinomial/categorical data, and some are binomial/dichotomous/boolean data. In my Javascript web application, I want users to be able to select a set of samples and see which layers are significantly different between the selected set of samples and all other samples. The end result should be a p-value for each layer, from a two-tailed test (where applicable) of the null hypothesis that the distribution over selected samples is the same as the distribution over unselected samples.
I've done the mathematical reasoning and determined that I want to use a Mann-Whitney U test for the continuous data, a Pearson's Chi-Squared test for categorical data, and a Binomial test (exact, without the normal approximation) for dichotomous data. All of these tests are available in the excellent scipy.stats library for Python.
Is there a Javascript library available with implementations of these tests? Failing that, is there a Javascript library that provides PDFs and PMFs and CDFs of the distributions that would be required to implement these tests, like the Chi-squared distribution, or the (discrete) binomial distribution? Failing that, is there a resource available that explains how these tests work with an eye towards implementation? Failing that, is there a library of basic mathematical functions for probability, like erf or the gamma integral?
I am aware of jStat, which seems to provide only a few continuous distributions with no API documentation, and of OpenEpi, which is more of a monolithically integrated epidemiological statistics system than a usable library.
This is not a full answer, just evidence to the negative.
Apart from never encountering such a powerful library I have googled a little and found no results other than jStat. However I have found a page (http://home.ubalt.edu/ntsbarsh/stat-data/Javastat.htm) where some client-side statistical calculations can be performed. This is not a library, and I have had a look at some of their javascript and it seems like they have coded the calculations by hand (which I wouldn't expect if a library existed)
So at best I offer some evidence to the negative.
(Also note the rather inconclusive post Recommend a good javascript statistics library?)
I wouldn't want to do heavy numerical integration in javascript though, can't you do that on the server-side instead, especially if the user is merely selecting data, and not entering it?

Working with Threads - UbuntuTouch/QML/Javascript

I'm taking my first steps writing an QML/javascript application for Ubuntu touch using qt creator.
Currently there i dont think there's too much documentation on this topic.
Can anyone point me out a good/clean way to work on multiple threads in this circumstance?
QML is not really designed with the intention of working within more than one thread. The origional intention was that any threading should be handled by the layer existing in C++. However, if you do really need acces to threads to perform things like calculations, and you are unable/unwilling to write code at the C++ level, there is the WorkerScript QML element that may be able to provide the funcionality you want.
https://qt-project.org/doc/qt-5.0/qtqml/qml-qtquick2-workerscript.html
Its worth note that depending on what you are trying to do this may or may not be entirely appropriate to use.

nodejs make image from smaller images?

I have some small images that tiled together make a fullsize image. The tiles are saved on the server. I would like to stitch the tiles together in the right position and create 1 image file on disk made up of all the tile files. How can I do this in nodejs?
Thanks
Your best bet is probably to invoke a tool like ImageMagick, which has a montage command that does exactly what you're looking for.
This would be fairly straightforward to implement yourself, but I see that this fork of node-imagemagick has montage support.
Since node.js doesn't have a graphics editing suite, your best path would be to
You could call an external script, using java, using php, or the language you feel most comfortable hacking with.
There's plenty of material on how to run a script from node.js, so I won't mess around with that here.
However, I would suggest that you pass a temporary filename as an argument to the script, then when it finishes executing, go get that file rather than trying to read back the binary as a return value or something equally convoluted.

Ajax-ish updating of a canvas element

Ok this is more of general technology/approach question.
We have a very simple web 'app' which is basically just a canvas element. We're using javascript to draw a 'fancy UI' on the canvas. We register a mouseclick event, get the coordinates and then try to figure out what widget they clicked on.
My question is, what technology or framework would be best for doing this in an AJAX-ish sort of way? I want the page to render, then user the user clicks somewhere on the canvas, and we pass that coordinate back to some server-side code(PHP or Ruby) which will run some logic, update a database etc., and then come back with new data for the javascript Draw method, effectively updating the UI based on what the user selected, all with a minimum of blinking and refreshing.(although the canvas can be in an IFRAME)
What's the easiest approach to this? I've looked a little at Rails, but it seems like overkill. All I want is to pass back the click coordinates, run some server-side code, and come back with a new chunk of text to pass my javascript Draw function(the one that draws on the canvas element)
EDIT: ok, to be more precise, I'm essentially asking about the easiest way to post back simple string data via AJAX(meaning no full page refresh), and receive a string return value, and update a canvas tag with that return value. I'm not asking for code or an example(although I wouldn't complain) I'm just asking for thoughts on the easiest tech to use - jQuery, Prototype, Rails, JSON vs. XML, etc. The back end is all PHP or Ruby, so I dont need help there, just with the AJAX->Canvas portion of it.
Umm, I suggest Ajax. Its very Ajax-ish.
Okay, let me elaborate - that last line isn't very helpful, but neither is the question. Use JQuery/Prototype to make an ajax request to your service with the co-ordinate and other information you've gathered from the canvas element. Take the response to update canvas depending on what the original input was.
You question is very non-specific... you cite "an AJAX-ish sort of way"... well, that can mean so many things. To me, why go with something thats ajax-like, when you have the real deal ajax.
"What's the easiest approach to this?
I've looked a little at Rails, but it
seems like overkill. All I want is to
pass back the click coordinates, run
some server-side code, and come back
with a new chunk of text to pass my
javascript Draw function(the one that
draws on the canvas element)"
Yup. We're still talking about Ajax.
Vote me down if you want, but better yet - clarify your question and let us know whether you're talking about how to implement passing data back and fourth, or whether you're looking for cutesy UI
effects...
If all you want to do is do some calculations why not also do it in javascript?
If you need permanence, or are running intensive calculations then any of the following will do.
Easy to deploy Frameworks: *
(That is to say, frameworks that have lots of documentation and tutorials available and are written in what are commonly considered to be easier scripting languages.)
CodeIgniter | CakePHP | Zend
Django
Harder to deploy Frameworks: *
(That is to say, frameworks that have less documentation available (and limited tutorials) or are written in more difficult languages [that is, languages that commonly use more difficult paradigms])
Multiple Lisp Frameworks
C++ Frameworks
Multiple Perl Frameworks
More Java Frameworks than you could ever have time to use
I suggest reading up on them, checking the examples and choosing the one that seems most intuitive. Write one or two examples of things you would like to implement and if it still seems intuitive (or the support and tutorials make it easy) then use it to implement your whole project.
*Please note that this division is completely subjective.

Is there a way to manipulate code generated by Processing.js via Javascript?

I would like to call some functions or alter some variables after the Processing.js code has been parsed. Right now the only way to do this is to manipulate the source code, and then reload the source into Processing.
I am beginning to suspect that there is no straight answer to that question at present. For the time being, I am doing as you do. I am using the Django web framework. I have Django write every line of Processing.js code to the page through its template handler. Django is first writing the HTML script element, then Processing.js assignment statements defining the sketch parameters, then the body code for the sketch, then Processing.js library functions and classes as a particular sketch requires, then the closing script tag. I’m looping through multiple sketches and finish up with a looping initialization routine.
With this scheme I have fine control over the HTML as Django’s template handler lets me control id’s and classes for the div elements that contain the sketches as well as the canvas elements. This amounts to crude dynamic behavior; every time the user POSTs a form the sketches are updated. Python is reading a lot of files, each containing a lot of bits, and those bits pass through several hands on their way to the browser, which must slow thing up as compared with a simple update of the data.
I'm intrigued at the possibility of genuine client-side dynamic behavior using forms mediated by, perhaps, jQuery. See this fascinating post at Design Intellection by David Yeiser. So, jQuery can talk to Processing.js, which is amazing, but not too surprising, as they are both JavaScript. I'm no expert, but I think some global namespace abuse occurs with this technique. Speaking of global namespace abuse, check out these two posts at Irrational Exuberance, "Updating Processing.js Graphics via Ajax," and "Using Javascript to Control Processing." If you can do it with PHP, you can do it with the flavor of your choice.
Apparently, Processing.js has an "internal" AJAX function that might be of use to you. See this cached post, courtesy Google. Compare with the current radically edited version, changed just a few days ago. So, the Processing.js collaborators have got the task of loading a sketch and associating it with a canvas down to two lines of code, but the more general task of loading assorted bits and pieces, including variable data, is getting no air time. Perhaps they're rewriting the whole AJAX thing. I would like to know if the p.ajax function could be used to load and concatenate a number of Processing.js assets, like data assignments, setup(), draw() and functions and classes in a flexible way forming a complete sketch.
My CS son says I could re-write my Processing.js code using the JavaScript native API and then it would be pure JavaScript, and could be loaded using a normal script tag and not with type="application/processing". He thinks I would have more flexibility plus the option to use minimization/obfuscation. Here is an example using JSON at ProDevTips. JSON is pretty convenient to use with Django. I've seen the API here and there on the web. It looks straightforward, but I don't know if it's documented anywhere. You could just pick through source at Github. Otherwise, I like this approach.
So, I don't have a succinct answer, but I suspect the situation is evolving quickly. One last possibility is the use of XHR. See this StackOverflow post and the link to the downloadable file at the Hyper-Metrix website. A notable Processing.js whiz by the name of Alistair MacDonald (F1LT3R) has published an number of approaches to initializing Processing.js sketches. He would probably know as well as anyone how to do what we want done.
I would be overjoyed if a well-informed person (i.e. a Processing.js insider) would address this issue.

Categories