Is there any way or tips to call a Python file (or functions) winthin a .js file (called on a HTML web page).
Indeed, I did the following tip :
- configure my server (it's Apache) to run Python files (CGI)
- created a Python file on the following structure :
print("<head>....</head>")
print("<body>....</body>")
But what I want is to call a Python file (a real one, with classes, functions or other things) on the Javascript. Any ways to do it ?
(something with Ajax or other stuffs)
Thanks, Clément
EDIT
Here's what I want to achieve :
my structure :
1) HTML/CSS/JS files to create an interactive chat
2) a Python file to process the answers on the chat
My goal
On each post on the chat, a Javascript function of my .js file is called. This function need to run my Python file, and my Python file is supposed to return something. The Python file is a pure Python file, no HTML or other things.
What I've done
I created a server thanks to XAMPP. Moreover and as I said before, I tried the thing with print("...") on each line, but it's by far not what I want.
EDIT 2
I already tried a thing with AJAX like :
$(".mytext").on("keyup", function(e){
if (e.which == 13){
var text = $(this).val();
if (text !== ""){
insertChat("you", text);
$(this).val('');
$.ajax({
type: "POST",
url: "test.py"
});
}
}
});
but I get an error :
The error I get is on the HTML console, in French :
Erreur d’analyse XML : mal formé
Emplacement : http://localhost/test/test.py
Numéro de ligne 1, Colonne 2
so in English:
Error of XML analysis : malformed
Path : http://localhost/test/test.py
Line 1, column 2
JavaScript runs in your browser. Python cannot run in your browser.
The Javascript code can create requests to a server. On the server you can run all kinds of code, including python. The python script(s) have to reside on the server and the server has to be configured accordingly (see WSGI, CGI, "How to use python on the web").
Edit:
Answer to updated question:
What you want is to create XmlHttpRequests from JavaScript to the python chat program running on the server. The data format will be JSON (not XML). This technique is called AJAX ("Asynchronous javascript and XML"). I suggest you read some tutorials on these topics. The web framework for Python is called Django.
Related
I have tried numerous different codes that I have found here along with the following code below I got from learn.microsoft.com. They all give me the same error though. The error I get is "ActiveXObject is not defined". Can someone please tell me how to fix this issue. How do I define the object or is this yet another problem that is related to my host, GoDaddy?!?!
This is for a Plesk/Windows server hosted by GoDaddy.
This is a link is to just one of the codes from stackoverflow that I have tried: Use JavaScript to write to text file?
Microsoft Code
<script>
var fso, tf;
fso = new ActiveXObject("Scripting.FileSystemObject");
tf = fso.CreateTextFile("G:\mysite\file.txt", true);
// Write a line with a newline character.
tf.WriteLine("Testing 1, 2, 3.") ;
// Write three newline characters to the file.
tf.WriteBlankLines(3) ;
// Write a line.
tf.Write ("This is a test.");
tf.Close();
</script>
You can't write to a file on the server with client-side JavaScript (if clients could write arbitrary files on servers then Google's homepage would be vandalised every second).
The code you've found could write to the hard disk of the computer the "page" was loaded on, but only if the "page" was an HTA application and not a web page.
The standard way to send data to an HTTP server from JavaScript is to make an HTTP request. You can do this with an Ajax API like fetch.
You then need a server-side program (written in the language of your choice) that will process the request and write to the file (although due to race conditions, you are normally better off using a database than a flat file).
Hi I'm new to programming and making a project exactly like these Real time GPS Tracker on JUST HTML / JS and Google Maps to be run on a handphone
My Next step is all users should see others location and one of the answers said
you'd have to send the points to a server-side script using AJAX.
I know how ajax works i just don't know what he meant by server-side script
$.ajax({
url: "sample.php",
context: document.body,
success: function(){
$(this).addClass("done");
}
});
My Questions is:
Is the code above consider a server-side script? if no any examples
Should i make new .php file?
You have a file that exist in you server so one cannot access it using their browesers.In your case that test.html file is residing in server. it can be .php file .js file ....
The code above is not a server side scripting language as the url is still point to a .html page.
A server side scripting language is a language that runs on the server rather than on the client. example of a server side scripting language is php.
If a browser plugin is sending messages to a .bat file, what is the correct way to send that message? I have seen the following examples:
var sending = browser.runtime.sendNativeMessage("ping_pong", "ping");
And
var port = browser.runtime.connectNative("ping_pong");
port.postMessage("ping");
In that example ping_pong is the name of the native application in the Windows registry.
Edit:
I am trying to use Native Messaging as described here:
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging
I have created a Firefox plugin which sends a string to a batch file using the code shown above. I have also created the registy entry and JSON file for the application as described in that link. The JSON file contains a link to the batch file so that the plugin knows what application to call.
The batch file can accept a parameter using %1. However the value of %1 is the name of the JSON file and not the message that was sent to it using JavaScript.
Edit 2:
It the example given in the link above it is a Python file that handles the standard input. Thus a batch file calls a Python file to handle the messaging. I used a Java file instead. This resolved the problem and it works without problems.
I'm using Netbeans 7.3.1, and usually I can successfully debug PHP files using Xdebug. However, that's only if my site project is structured in such a way that the site is generated from PHP code right off the bat.
Right now I have a site which is mostly made from HTML files. The HTML static, not generated by PHP. The only PHP file in the project is called phphandler.php. I need to debug that PHP file, but it only runs in response to a Javascript/jQuery call from within the HTML files that looks like this:
$.post(siteURL + 'jsonhandler.php', {
JSON: JSON.stringify(data)
}, processResult, "json");
On the PHP side, it processes the JSON request from $_POST
$Array = json_decode(stripslashes($_POST['JSON']), true);
When I run the debugger from within JSON, it opens Chrome and loads my site, starting with index.html, but the debugger never opens jsonhandler.php. I can go through all the links and navigate through my site, and it will get all the JSON data it needs from jsonhandler.php, but the debugger remains uninvolved.
Can I debug through jsonhandler.php when the Javascript sends it a request via $_POST, and if so, how?
Update: I've discovered that I can debug jsonhandler.php using the Debug File function, but this has serious drawbacks, namely that I can't create a situation in which jsonhandler.php is receiving JSON data as constructed by the rest of the site. I can manually force in some hand made test JSON data, but then that seriously inhibits my ability to discover what the system is doing as a whole.
Anyway, for me this is another indicator that the debugging environment is configured correctly, it's just a matter of if and how the debugger can listen for the right events.
What I normally do is putting break points. Try putting breakpoints to the PHP code that receives the request (or that you want to debug) in the jsonhanler.php file and them run in debug mode, navigate through your site and send the request, the debugger should open Netbeans and take you to the line where you left the breakpoint. From there you can now go line by line or jump into functions.
You can try printing the $Array received in the php file by print_r($Array) followed by an exit() which might stop the process in the php file and show you the result coming in the php file. This might allow you to see as to what the php file is getting when it is loaded in the $Array. Or else if its an ajax request you can print the response in the console by console.log(response). I hope it helps.
I recently responded to a similar question here: https://stackoverflow.com/a/19636910/212076
Basically, you need to add a query parameter within your JSON to trigger the debugger:
XDEBUG_SESSION_START=netbeans-xdebug
If the data is being obtained directly from a HTML Form, then include a hidden field like so:
<input type="hidden" name="XDEBUG_SESSION_START" value="netbeans-xdebug" />
I have a very simple Python file, called python1.py, whose contents are:
f = open('C:\\Temp\\test.txt', 'w')
f.write('Succeeded')
f.close()
I wish to execute this from JavaScript, like so:
jQuery.ajax({
type: "POST",
url: "/cgi-bin/python1.py",
success: function (msg) {
alert("Data Saved: " + msg);
}
});
However, all that happens is that I get an alert showing me the contents of the Python script. The file C:\Temp\test.txt does not get created, so clearly the Python was not executed.
How do I persuade the code to execute the Python script instead of just reading it?
You simply need to configure your web server to execute your *.py scripts, instead of serving them as plain text.
If you are using Apache as a web server, you need to enable mod_python or mod_wsgi.
EDIT:
Since you are using using Apache, you may want to check the following article, which briefly describes how to set up the mod_python module:
A Brief Introduction to Apache's mod_python Module
You could also use the opensource project Pico. It's a really elegant way of calling server side Python code from client side Javascript.
The author has provided some simple examples here https://github.com/fergalwalsh/pico/wiki/Example-1:-Hello-World
Are you able to execute the script directly from the browser. This looks more like a webserver config issue than jquery's
If your script is that simple, you would be best off using CGI on the server side rather than mod_python or mod_wsgi as suggested by others. For details on how to set up Apache for CGI with Python and simple script examples see:
http://webpython.codepoint.net/cgi_tutorial