Python Forum

Full Version: Post Python to HTML/CSS
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I'm trying to build a GUI for a python script I have using HTML/CSS, and I'm wondering if it's possible to link python to my HTML page.
For example, I would like the current time and date to be displayed on my HTML page but I don't want to use JavaScript. Instead I want t use python. I want to have python's date-time function posted to the HTML page.

Is that possible?
If so, how would I go about doing that?

Thanks in advance.
Flask or Django are the main packages for webpage development.

I have always used Flask, so not able to comment on Django other than to say it's more popular than Flask, but probably not as flexible.

I used Miguel Grinberg's tutorial as my intro to Flask and found it outstanding (my opinion).
Note that HTTP works using a request/response cycle - the browser makes requests to the server, which responds with information (e.g. the HTML, etc. in your case). Python runs on the server and if you're going to have to make requests repeatedly, you may find that's not the best for performance (and even for usability, if you're reloading the page every time).

When you want to do programming in the browser, JavaScript is all they understand. These days, that doesn't mean you have to write in JavaScript, as other languages can be made to target JavaScript. For Python, there's Brython, but I've never used it and don't know what it might lack.
An other approach to this htmx make stuff easier and get away with not writing JavaScript.
That said if gone do web-development some JavaScript knowledge should be standard.

Can give a example all in one file,not ideal but show the simplify of htmx with Flask.
This also dos AJAX no reload of page as you see it's only html(with a twitch htmx).
from flask import Flask, render_template
import pendulum
 
app = Flask(__name__)
@app.route('/')
def index():
    return '''\   
<head>
  <title>Some title</title>
  <script src="https://unpkg.com/[email protected]"></script>
</head>
<body>
  <h1>Start page</h1>   
  <button hx-get="/test" hx-target="#output">Click Me</button>   
  <p id="output"></p> 
</body>'''
 
@app.route('/test',  methods=['GET'])
def test():  
   now = pendulum.now() 
   return now.diff_for_humans() 

if __name__ == '__main__':
    app.run(debug=True)
[Image: 49Jjuc.png]
Also can not just run a loop on server that feed this from datetime or Pendulum as i use here.
Can eg look at this post where use BackgroundScheduler.
Or an other approach here.