Where do I add the ". py" calculations to the HTML page?

Asked

Viewed 80 times

0

Speak, people! Blz? I’m starting out in the area and I’m having a lot of questions. I set up a basic HTML test page and wanted to insert a python code I created (test calculation).

Where and how I link between HTML and . py code?

Thanks for the help.

  • You would have to use a python framework for the web, I suggest starting with the Flask or even the Django

  • Another python webframework Voce can try is the cherrypy

2 answers

3

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

2

Python programs are not normally added directly to an HTML page - they can Beget HTML pages or data that will be rendered by javascript code.

You interact with your Python programs from the terminal, making programs that create a graphical interface, or programs that generate the full HTML - filling in values with the use of templates - you can take a look at the project "Flask" to some simpler examples of how to do this.

It is even possible to embed Python code directly within HTML, as is done with Javascript, with the use of alternative projects - one of them is Brython (https:/brython.info) - it’s not the way most people study Python, but it might work if you have it in mind.

Even going through the Brython route, it’s important - vital - that you study and understand the normal Python code relationship with web pages.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.