Question:
I have searched far and wide, but nowhere have I been able to find a set of instructions I actually understand.
How do I use Python in an HTML website?
Python isn’t really meant for HTML. You can use PyScript, but I don’t know how reliable or effective that is. You can also use a Flask server to host HTML pages, which allows you to generate HTML pages from Python variables and whatnot. What is your objective?
I’ve tried that, it’s not really… idk… good? usable?
this is what I’e seen while searching. How exactly would I do this?
Here’s the template for Flask: https://replit.com/@replit/Flask?v=1
Here’s a nice guide written by Qwerty: Python Flask Jinja Templating
Basically, Flask allows you to create HTML pages using Python. So, building on the Replit Flask template, this would be a basic website:
main.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
my_title = "Welcome to my Flask app!"
return render_template("index.html", title=my_title)
app.run(host='0.0.0.0', port=81)
templates/index.html
<h1>{{title}}</h1>
This would make https://[repl-name].[username].repl.co/
show an <h1>
element that says “Welcome to my Flask app!” I personally found Flask very intuitive. I began using it after learning the basics of Python. You also don’t have to use render_template
and can return
HTML code but render_template
is a good choice for larger sites.
@CoderElijah could I use this in a alrey made HTML site?
could I use this in any way to make sort of a display box on the HTML page to the side of other content, where I can run the py?
No because it’s a server.
so if I have a website, the only way I can add py to it is with pyscript?
Pretty much. There might be something else like PyScript but yeah adding Python to a website would require something like PyScript. Flask builds websites, but it is backend (like NodeJS), not frontend.
Here is a pretty good option:
pywebio
PyWebIO Application?
simple to use just do
import pywebio
from pywebio.input import * ## enable all the input features
from pywebio.output import* ## enable all the output features
from pywebio import start_server
def some_stuff():
put_text("woah neat")
input("do you like it?")
start_server(some_stuff)
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.