0

I have this issue I can't resolve for myself and all the other advices already present I could find were not helpful at all. I'm using http.server and socket server packages and I want to transfer variables from main .py file to html pages. How do I do that? I've even found about some post methods, but even then, there was no answer as to how to actually get those variables in the html.

thanks for any advice

Micca
  • 9
  • 2
  • normally server should generate all HTML with all needed values and send full HTML as one string. And if you want to add elements without reloading web page then you have to learn JavaScript (AJAX) and JavaScript's function `fetch(url, ...).then(resposen => {...}).catch(error => {...})` to get data from server (python) and other JavaScript's functions to replace/add elements in HTML in browser. – furas Oct 21 '22 at 01:47
  • I dont want to add elements I just want some "ifs" in the html page in javascript and those "ifs" would decide based on values I send them from .py file, thats what I would like to do – Micca Oct 21 '22 at 15:11
  • if you want to use values at start (when you generate page first time) then it is simpler to put data directly in HTML before sending to browser (without using JavaScript). If you want to use values when page is already in browser (and you click button or select some value on page) then you have to use `fetch(url, ...)` to send information to server (to Python) and get values from server (from Python) and next you can use `if/else` in `.then(resposen => {...})` to decide what to do with these values. – furas Oct 21 '22 at 20:14
  • BTW: code on server (Python) and code in browser (JavaScript) are two separated elements and they have to use `fetch()` to communicate. Other thing is: it works as `Client-Server` and browser (as Client) has to send `requests` to get `response` from Server with values. Server can't send values to Client if Client doesn't send `request` to Server. Server could send data to Client without `request` from Client only if you use new method - `websockets` - and this is more complex. – furas Oct 21 '22 at 20:23

0 Answers0