I don't think that this idea/requests will gain any traction or replies that are positive.
JavaScript is to deep into the browser HTML DOM implementations
Brython ok try,but i don't thin that is the right way forward.
I think the
PyScript is a step forward in right direction.
So can run Python code in html example with a Python function.
Need no server,just run as eg
joke.html
.
<head>
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<py-script>
import random
def joke() -> str:
"""Return a random joke"""
lst = [
"I've got a really good UDP joke to tell you but I don’t know if you'll get it.",
"Debugging: Removing the needles from the haystack.",
"# This line dos nothing,but the code stops working when I delete it.",
]
return random.choice(lst)
print(joke())
</py-script>
So one of the problem now is that runtime load time is to high,
saw that they did test with
MicroPython which will give much lower runtime.
MicroPython + Pyscript: Is this finally Python on the Frontend?
htmx is for me one the best new tool out there for simplify web-development.
To give a simple example all in one file,
so this dos call(AJAX call no page reload) when push button to get new joke,see that no JavaScript is used all is done in HTML.
from flask import Flask, render_template
import random
app = Flask(__name__)
@app.route("/")
def index():
return """\
<head>
<title>Some title</title>
<script src="https://unpkg.com/[email protected]"></script>
</head>
<body>
<h1>Get a random joke</h1>
<button hx-get="/joke" hx-target="#output">Get a Joke</button>
<p id="output"></p>
</body>"""
@app.route("/joke")
def joke() -> str:
"""Return a random joke"""
lst = [
"If Bill Gates had a dime for every time Windows crashed ... Oh wait, he does.",
"Debugging: Removing the needles from the haystack.",
"# This line doesn't actually do anything, but the code stops working when I delete it.",
]
return random.choice(lst)
if __name__ == "__main__":
app.run(debug=False)
One more with
PyScript,so can use many libraries they build in.
<head>
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<py-env>
- numpy
- matplotlib
- pandas
</py-env>
<py-script>
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.DataFrame(np.random.rand(10, 4), columns=["a", "b", "c", "d"])
fig, ax = plt.subplots()
df.plot.area(ax=ax)
plt
</py-script>