Skip to content
← Back to Community
How to add python to HTML
Profile icon
Mendelevium

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 [earned 5 cycles]
View Answer
Voters
Profile icon
AnaSofiaGarciaR
Profile icon
AphixDev
Profile icon
Commodore95
Profile icon
Mendelevium
Comments
hotnewtop
Profile icon
AdCharity

@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

Profile icon
Mendelevium

Thank you so much for the link, @AdCharity

Profile icon
AdCharity

@Mendelevium no problem :)

Profile icon
RiaDebnath1

@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? :)

Profile icon
AdCharity

@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
Profile icon
RiaDebnath1

@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?

Profile icon
AdCharity

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

Profile icon
Ruchikasliwal

no, even i wanna know. forget grammar

Profile icon
AustinBPark

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

Profile icon
Vandesm14

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

Profile icon
AphixDev

@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

Profile icon
Vandesm14

@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.

Profile icon
Zavexeon

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