Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
My goal:
A running python keeps changing the value of a variable.
Real-time showing the changing value on browser using html and javascript as long as the python is changing the value.
The following is my googled solution:
Write the python code using Flask
Request to run the python via Ajax from javascript
The python responds the variable as JSON back to javascript
Base on this solution, I have the following questions and feel free to correct me if there were any misunderstandings.
The python has to be executed manually first, then the javascript can send a ajax request to http://localhost:5000/[...some #app.route()]. I need the python starts running infinitely(while loop) after the first ajax request.
The Flask python can not have a while loop. Not sure the mechanism of app.run() in Flask.
Once the first point is achieved, the javascript also has to keep updating the html once the python is changing the variable. Now it seems that I have to keep sending ajax request to get the variable from the python.
Really appreciate any information to address my questions!
Basically, you need three parts
your python loop A
a web application B
and a javascript application C
You can combine A+B into one process, in which case you'll have to run the loop in a separate thread, or you can use any IPC means available, like shared memory/pipes/sockets, to communicate between A and B. A simple solution would be to use a database server, to which A writes and from which B reads.
If A and B are separate, then B doesn't have to be python, you can write it in js or php or everything else.
To avoid polling on the client side, you can use WebSockets to communicate between B and C.
You will have to run the while loop in another thread or process, for example using flask-celery
the javascript updating can be achieved with setTimeout
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Good night guys!
I'm new to programming and now I'm studying the $ .post () method and I have a few questions:
1 ° - Why should I use the $ .post () method?
2 ° - In all the examples I see, they always use the $ .post () method to send something to PHP ... Is it possible to send something other than PHP? If so, could you give me examples using only JQUERY, HTML, mysql and JSON? (that I'm studying)
Note: I'm a beginner.
$.post() is a jQuery method that allows you to send a request to a server (typically) with data that can be processed by the server and sent back.
PHP is a programming language that can run in a server, accept the $.post() request, and process the request.
$.post(), the jQuery method, is used in the "front-end" of web apps. The front-end is the part the user uses. So, a typical example of the use of a $.post() request would be a user on a social media app adding new images to their profile. The image is transformed into data (bytes) and sent from the users computer to the server with the $.post() method.
That's where PHP comes in. PHP will receive the data bytes and save it to the users profile. All the magic of making sure we have the right user and pulling up their info then adding the new image to their profile actually happens here. At the end of it, typically a "success" or "error" response is sent. For reference, when you get a 404 error or a 500 error, that is where the error came from. A server.
The front-end of web applications is very limited. It only has JavaScript, jQuery (which is just a sub-set of JavaScript), HTML5, CSS3, and some altered versions of these.
The back end, the server that handles all the requests and sends all the data back, can be done in a plethora of different languages. PHP is an older language but it is still actively updated and still used in a lot of legacy apps. We now have Node.js, Java, Python, etc. A lot of different languages to write a back end.
Hopefully this clears some things up for you. Please feel free to ask me questions in the comment section or send me a message if you don't have enough reputation to comment yet (don't know the numbers lol).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
My issue is that I need to send a TCP message from a web page. This is not possible for security reasons, so I have written a C# function in a program that is already running as part of my production environment to send the TCP message, and I want to invoke that method via the web page. I basically need to host a web service and call a C# function in that web service from javascript in a web page. How can I do this simply?
I have looked around at solutions like asp.net (my web page is part of a GIANT web client that does not and will not be able to utilize .aspx files), and WCF (which seems like overkill for simply sending a TCP message), but none seem to really nail down my intent of calling a C# function in a project that I have already made, from a web client that is already large relatively unchangeable.
Note that what I really want to do is be able to call C# code in a project I've already made and plan to deploy, so what I'd love to be able to add REST functionality to a method there, but I am also very open to simple/smart solutions outside of my existing project to an extent.
Thanks for any guidance!
You have a lot of options, but I would go for a web service, and my own personal preference is an ASP.NET MVC Web API. I don't know exactly which direction to send you down, but you can start from here and go further. It's possible to write a RESTful Web API using ASP.NET MVC and it's a relatively mature product with a large number of users.
http://www.asp.net/web-api
http://www.asp.net/web-api/overview/older-versions/build-restful-apis-with-aspnet-web-api
As for calling the Web API method, you can use any sort of AJAX call. I usually use jQuery's AJAX methods, myself.
The best way will be direct call ajax to your app through a soap/rest web service.
There are also some alternatives for calling server apps from website:
You have as many options as server side services in the world. The simplest, in my opinion, way is to make an ajax call to a php file which will run a call to your C# program. On the other hand it won't be the best solution in case of performance. You should definitely consider using NodeJS as it is a perfect technology to make a server side 'server' that will handle tasks for server and works well with browser calls (through ajax or socket.io). If your TCP message isn't too complex you can rewrite it in Node, or just call the C# from the Node app.
PHP solution:
Just use a file that will execute your program using for instance http://php.net/manual/en/function.exec.php
NodeJS:
there is a great gist which shows how to send tcp messages:
https://gist.github.com/tedmiston/5935757
Ajax calls:
You can use either pure javascript (http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp) or some libraries like jQuery http://api.jquery.com/jquery.ajax/
Socket.io:
simple socket.io example: What is an example of the simplest possible Socket.io example?
Beware: you need to secure the access, leaving ANY way to make an unauthorized call to a server application is highly insecure.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I got a project to design a website which has a javascript & HTML interpreters, Database, C++ compiler. I have searched related websites and I ended searching with codecademy
. Now I'm thinking to build something like codecademy website with my own features and functionality. Although I know how to create a dynamic website but I don't know how to embed interpreters, compilers and all. Please guys help me to build this project and successfully run
In very simplified terms, there are two ways to achieve a live interpreter/compiler on a web site:
Find (or write) a compiler/interpreter for your language that is written in Javascript
Create a (native) CGI application and run it on your server that will run a given piece of code through a (native) interpreter/compiler (also on your server) and return the result of the run. Then write a web page that calls on that CGI to do the work, usually using AJAXy Javascript things like xmlHttpRequest, or at least a web form.
That's what all of these sites essentially boil down to. Some just have an interpreter or CGI that can remember state between calls, and can then show you intermediate steps. This could be because your JavaScript-based interpreter supports pausing, or your CGI could e.g. have the program run in lldb and then your page sends certain commands to LLDB and returns information.
Note: If your question was more about how to actually write an interpreter in JavaScript, I detail the basics of parts of an interpreter here: http://orangejuiceliberationfront.com/how-to-write-a-compiler/ It is for C programmers, but the basics hold true in any programming language. You can always use arrays instead of memory blocks, and array indexes instead of memory addresses.
Simply web application takes our code as input and store in file with respective the programming language extension like .java, .c etc and then they used the server side install compiler to run the code and forward the output to the client through web page.
Suppose in java, there is class Runtime and Process with the help of this class we get the control on server side install software like java, javac etc with that we run the program and transfer the output to User..
It is very simple if any one want code for that then ping me..
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to accomplish the following:
Pull information from a module through Python. [Accomplished]
Constantly pull information from Python for use in HTML.
Avoid writing the entire HTML/CSS/JS document in print statements.
I've seen the term CGI thrown around, but don't really understand how it works. Simply put, I want to run the Python script which returns an integer value. I then would like to take that integer value into JavaScript so that it may update the designated tag. It should be able to execute the Python script every two seconds, receive the output, then apply it to the page. I do not want to write out the entire HTML document in Python by doing one line at a time, as I've seen some people doing on sites I've found.
It seems like doing something like this is far more complicated than it should be. The page should run, call the script for its output, then give me the output to use.
Well if you don't know what CGI is and find that what you ask for is "far more complicated than it should be", you first have to learn the HTTP protocol, obviously, and that's way to broad for a SO answer.
Basically what you want requires:
an html document
some javascript code, either linked from the document or embedded into it
a web server to serve the document (and the javascript)
a web server (can of course be the same as the previous one) that knows how to invoke your python script and return the result as an HTTP response (you'll probably want a json content type) - this can be done with plain CGI or with a wsgi or fcgi connector, in your case CGI might well be enough.
Then in the browser your javascript code will have to issue a GET request (ajax) every x seconds to the web server hosting the Python script and update the DOM accordingly.
This is all ordinary web programming, and as I said, a basic understanding of the HTTP protocol is the starting point.
Trying to write anything from scratch, if you don't know anything about the subject, is always going to be complicated.
That is why there is a whole world of tools to help you. I don't think you want CGI at all; look into one of the Python micro frameworks, in particular Flask. The tutorial there should give you the introduction you need.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I wrote a basic python 2.7 game using things like print, for, if, else, etc. I also made a website to hold the game downloads. I was wondering if there is a way to embed the python game directly into the HTML5 website. All of the other questions that I looked at contained links to non-existant websites.
Depends on what you want and how the game does I/O, but I'd check out pypy.js, it's a version of pypy running in the browser, capable of basic I/O and what not, that would require a minimal amount of reworking your current setup. For faster loading, you could use the same toolchain as pypy.js to compile your python code to C, and the C to asm.js. That would require reworking your code to valid RPython, which requires runtime variables to be statically typed and a few other restrictions. It would allow your game to run in the console at near native speed.
If you want the game code running on a web server, a COMET/AJAX solution is the way to go. I usually use Athena LivePage from the Nevow for that type of thing. A 'drop-in' solution would be to use something like ShellInABox or PyInABox to run a server-side shell, and the run your python script inside the shell, running as a no-permissions user inside a chroot.
As I said, I'm not familiar with javascript (I have some experience in java though), but here's what I see you would have to do: Redirect print to a separate file, not sys.stdout, send to an output stream, and have javascript display the information received in the stream in the browser.
About javascript, I'm not entirely sure.
But here is a little guidance for your python code:
print "Hi! I'm Zin"
to...
print >> (destination file), "Hi! I'm Zin"
print >> sys.stderr, "Hi I'm Zin" # Will print to Python's built in error stream.