Ask coding questions

← Back to all posts
How to add python to HTML
Mendelevium (41)

Hi, I've looked up different ways to add python to HTML, but none seem to work. Does anyone know how to add python into an HTML page?

Answered by AdCharity (1317) [earned 5 cycles]
View Answer
Comments
hotnewtop
AdCharity (1317)

@Vandesm14 While it isn't exactly possible to add python to HTML, it is very possible to mix programming languages, such as making web servers with python. For instance, here: https://repl.it/talk/ask/Mixing-languages-in-one-repl/7063
Be sure to close out this post (meaning mark the official answer) if you got your answer :) Btw. this is kind of a duplicate of https://repl.it/talk/ask/Mixing-languages-in-one-repl/7063
The easiest way to put this is basically:

from flask import Flask, render_template
//import flask for werver
app = Flask(__name__)

@app.route('/')
def index():
	return render_template('index.html')
//get index.html
app.run('0.0.0.0',8080)
//repl will automatically create the website 4 you :)
//BTW idk what python uses to comment, but I hope this will suffice

In addition, you could technically make an iframe of a repl with python.... but that's kind of fishy

Mendelevium (41)

Thank you so much for the link, @AdCharity

RiaDebnath1 (0)

@AdCharity Hello, when I use this I get 'Internal Server Error' and

"The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application."

any idea how to fix? :)

AdCharity (1317)

@RiaDebnath1
well to start // are commas for javascript so you'd have to change those. Also, there is a specific way to set up the "render template" function. The file structure will have to look like this:

index.py (or server)
+--- templates (folder)
     +--- index.html
+--- static (folder)
     +--- style.css
     +--- script.css
RiaDebnath1 (0)

@AdCharity I'm so sorry but I'm doing this and still getting an error? Can I please add you to collaborate on the program?

AdCharity (1317)

@RiaDebnath1 Hey please check out the official flask documentation, there's some quick start examples.

AustinBPark (18)

what I would do is embed a repl.it output or use an iframe. But i dont think that works with graphics libs.

Vandesm14 (2645)

There is no way to add python to HTML. Only JavaScript works with HTML.

AphixDev (217)

@Vandesm14 That's not true at all. Python is a server side language originally created for HTML. Here's some reference:
https://www.quora.com/How-do-I-incorporate-Python-into-HTML

Vandesm14 (2645)

@nt998302 But it's not "to HTML". JavaScript is the only language that works with HTML. While python can be used as a server-side language, that's not truly pairing python with HTML, which is what he was asking.

Zavexeon (1188)

Hmm... you'd need to use a template engine. Look at this stackoverflow thread.
https://stackoverflow.com/questions/5168588/can-python-be-embedded-in-html-like-php-and-jsp