Python in HTML Code - javascript

I have a Python file, called function.py, which contains this code:
def double(x):
return x * 2
I also have this HTML code:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
alert(double(4));
</script>
</body>
</html>
Is there a way for me to load the Python file in HTML of JavaScript, or is that impossible?

No. You cannot execute python directly in your web page. The web page is in the browser and the browser does not know how to execute python.
So, the python needs to run on the server. You have a couple options for how you can access the python on the server from your web page:
You can use a template system that allows you to insert python generated results in the web page BEFORE it is sent to the browser so when it gets to the browser, your python code has already been run and the results were placed into the web page for your page Javascript to access in the browser.
You can make an Ajax call with Javascript from your web page to your server and you can code your python server to respond to that Ajax call and return results. The Javascript in the web page can then do whatever it wants to with the results of the Ajax call.

Related

How to do http request to get the whole source page when part of html loaded by javascript?

I wait to get the html web page from https://www.collinsdictionary.com/dictionary/english/supremacy, but part of the html file is loaded by javascript. When I use HTTP.jl to get the web page with HTTP.request(), I only get part of the html file that loaded before the javascript been run, so the web page I get is different to the web page I got from Chrome. How can I get the web page as same as Chrome get? Do I have to use WebDriver.jl with is a a wrapper around Selenium WebDriver's python bindings?
part of my source:
function get_page(w::word)::Bool
response = nothing
try
response = HTTP.request("GET", "https://www.collinsdictionary.com/dictionary/$(dictionary)/$(w.org_word)",
connect_timeout=connect_timeout, readtimeout=readtimeout, retries=retries, redirect=true,proxy=proxy)
catch e
push!(w.err_log, [get_page_http_err, string(e)])
return falses
end
open("./assets/org_page.html", "w") do f
write(f, String(response.body))
end
return true
end
dictionary and w.org_word are both String, the function is in a module.
What you want is impossible to achieve with just HTTP.jl. Running the Javascript part of the page is fundamentally different -- you need a Javascript engine to do so, which is nothing simple.
And this is not a unique weakness of Julia's HTTP:
Python requests.get(url) returning javascript code instead of the page html
(recently the standard library request in python seems to added Javascript rendering ability)

How do you run a server-side script without it being sent to the browser at all?

I understand that the title is a little confusing, but essentially I want to make my website run a php file on the server side that uses arguments from the html e.g
document.getElementById("example").value;
So I'd like to run it on the server but not have it linked to the html file. Is this possible?
I suggest you create PHP scripts on server side and reach them with XMLHttpRequest (tutorial here) or AJAX with Jquery for example.
PHP scripts you need, don't have to generate any HTML code : they just would process data.
In your JS, you get your values from the page, then you send your data to the server.

Executing a Python Script when Web Page is loaded

I'm trying to have it so that when my web page is loaded, a python script is executed. The website is run on an apache server and the script is in the same directory as the index.html (it's a very small project).
Is there anyway I can do this? I'm not trying to output the data from the python file to the webpage, nor am I trying to affect anything client-side, I simply want the python script to execute and do it's thing whenever the web page is loaded.
Is there some sort of javascript function that I can use? I've searched around but really haven't found anything similar. Thanks!
Hopefully, your web server is already set up to run Python scripts. Presumably it will recognize an index.py file as any other index.(html|cgi|pl|etc.) document. Create the following as index.py in your DocumentRoot:
my_webpage = """
Content Type: text/html\n\n
<html>
<head>
<title>Python-generated text!</title>
</head>
<body>
<h1>I just made an HTML page with Python!</h1>
</body>
</html>
"""
print(my_webpage)
# now that that's out of the way, let's run our script
import stuff
def myfunc():
# awesomeness goes here...
and make sure it's executable. Now, whenever a user requests http://www.yourserver.com/ the server will run index.py, which prints out the HTML content, including the headers, then goes on to run the rest of your script.
I had to do this too. I had a python script(which gets me data from another website) which gets executed when you click on a button.
I used Ruby on Rails for my client side code. I embedded the script file in my ruby controller which then gets called by my form and hence it gets executed.
eg:
cmd = " python getData.py "
exec( cmd )

Python send to javascript

My question is really simpel, but I haven't found it yet.
I have a python script running on my server. This script reads NFC cards. (pyscard)
Everytime when there is a card the cardID must send to a webpage. I have in index.html file on my localhost.
<head>
<title>Hello!</title>
</head>
<script language="JavaScript" type="text/javascript">
function card(Cardid){
alert(Cardid);
}
</script>
<body>
</body>
</html>
There is a way to do it by selenium (driver.execute_script(command) ) Selenium is to heavy. Is there a other simple and light way to do this?
If I got you right, you need to implement AJAX. Here is simpliest example How to implement a minimal server for AJAX in Python?
If you'll need something more complex — chose one of the python web frameworks (Flask or Django) and look for tutorials how to implement AJAX with them.
If you want to push cardId to webpage you need tot use something in between like socket.IO then on a listening channel pushing the data to all websocket clients. From there you can pass it to a function like yours.
The other way to do it is a bit more limiting.
You can make Ajax call to python which reads current card holding data and use that id. But when another card is on the reader,you then have tot refresh to see the new cards data.

Script run on server or client?

I have a thttpd server set-up which has the following html file. When I give address server-address/file-name.html on a standard web browser errors on the script is logged in error console of browser. I am confused about where the script is run actually? Is it on the client side or are the error messages just passed on to browser by server?
My requirement is to run script on a server to generate dynamic web pages upon client interaction.
<html>
<head>
<title>Entitled Document</title>
<script language="JavaScript" >
Function Java_Scriptfn()
{
alert('Test'
}
</script>
</head>
<body>
<input type="button" value="Script_Check" onclick="Java_Scriptfn()">
</body>
</html>
That is purely client side code, so it runs on the client.
As far as I can tell, thttpd only supports server side programming via CGI.
JavaScript that is embedded in a HTML site (either inline or load from another file) is always executed client-side (that means in your browser).
If you want it to be executed, server-side, you need something like node.js.
It's client side code; any Javascript files included in an HTML page will run client-side (although they can talk to a server, that's different).

Categories

Resources