Python scripts do not run directly on the "frontend" and even if someone says it is possible, it will not in fact be what happened, what can be at most is that executed something like the @utluiz response:
In this case he used the https:/brython.info/, but that’s not even really python, is just an "interpreter" that gets very close.
What you need to understand is that browsers interpret NATIVELY only:
- HTML
- Miscellaneous XML (SVG for example, sometimes depending on some plugin)
- css
- Javascript
- It has codec for some image, video and audio formats
- Some others like "subtitles"
And each browser has its own interpreter of each thing
If you want to use real Python and not things like Berry or some implementation with XMLHttpRequest
to simulate what you need to understand is where your browser is and where your server is, summarizing you need to understand the least of what HTTP is and how your browser communicates with it in the beginning (JUST IN THE BEGINNING) of this answer I have already made the introduction of how the communication browser and server works:
I say this because they cited in the comments and in the other reply things like Flask and Cherrypy, it doesn’t do much to just quote them, because most people don’t even understand HTTP right, incredibly there are a lot of people who think they just come and read an answer or article and think they will master the basics of a concept, protocol or technology, it’s not like that, in fact a lot will depend on practice, which takes time, and even with my answer here will not master the basics of truth, but at least will understand where is your browser and where is the server, which is what matters FOR A START.
Going back to Flask and Cherrypy and any other web framework, without understanding what HTTP is or where this front-end and back-end then has no way to proceed, if you read the answer that Linkei then probably understood the basics, then let’s get to the point, creating something with Flask and "host", something like:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return '<b>Hello</b>'
When accessing the local address (in a localhost test) will be made a DOWNLOAD of the contents to your computer, something like:
HTTP/1.1 200 OK
Date: Mon, 17 Mar 2020 11:28:53 GMT
Content-Length: 12
Content-Type: text/html
<b>Teste<b/>
Note that by default he returned Content-Type: text/html
and the browser will ASSUME that it has to process as HTML, this is called header, and even if the content did not correspond to what was said in the header the browser will assume as what was said in it, to summarize the PYTHON generated an HTML, ie Python runs in back-end and the chosen framework generates the content.
You could even generate images, videos and other things and return with Flask, this would be downloaded by your browser and if it is able to process in the browser it will display, you "don’t see" the download occurring directly, but it always occurs, anything you access on the internet is downloaded to your computer, get it in "memory" or cached
You would have to use a python framework for the web, I suggest starting with the Flask or even the Django
– Sidon
Another python webframework Voce can try is the cherrypy
– Sidon