How to code html in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12

How to code html in Python

5th Mar 2021, 1:11 PM
Melekte Petros
Melekte Petros - avatar
12 Answers
+ 3
Last week I was thinking of a code where I'll take user input in python prompt and then rather printing it in prompt, I'll do it in Web browser. I'm sharing the technique I've used and wish that it might help you. # create two or three variables where you'll store your html, css and if needed js code html = """ --- html code --- """ # or html = """html code part 1... <p>""" + whatyouwanttoprint + """ </p> ... html code part 2 """ css = """ --- css code --- """ with open ("test.html", "w") as t: t.write(html) with open ("test2.css","w") as tt: tt.write(css) #at this point if you run your code, then you'll see a test.html and a test2.css files are created. Notice here, without creating a css file in py, we can just write and save the css file in our directory manually, because our puthon code has got nothing to update in the css file import webbrowser as wb wb.open("test.html") # i wish it will work, otherwise please inform me what's going wrong. Thanks!
7th Mar 2021, 6:55 AM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
+ 4
triple quotes are not mandatory, but let you write multilines strings... inside triple quotes, you mostly doesn't need to escape quotes (unless they are the last character of the string or you need to output 3 or more consecutives) appart from building your html string from scratch, you could use templating engines: https://wiki.python.org/moin/Templating
5th Mar 2021, 2:10 PM
visph
visph - avatar
+ 4
Hey Melu you can see my python code I have written html in python and make glassmorphism effect https://code.sololearn.com/cF5HF4d046rk/?ref=app 🤗😊😃THIS WILL BE HELP FULL TO YOU😃😊🤗
7th Mar 2021, 5:48 AM
Anurag Kumar
Anurag Kumar - avatar
+ 2
You can't code html in python directly. They are two seperate programming languages. If you want to create a simple html page with a python script, you can do the following: html = """<!DOCTYPE html> <html> <head> <title>Demo</title> </head> <body> <!-- write your html content here --> </body> </html>""" f = open("demo.htm", "w") f.write(html) f.close() NOTE: the triple quotes are mandotory and any other quotes inside the code should be escaped! With this python code you are creating html files, but I do not recommend it. If you want you html to load dynamicly, use JS, PHP, ReactJS, NodeJS or Angular instead. Hope this helps. Good luck! EDIT: As visph pointed out, the triple quotes aren't mandatory. But your html will be way less readable as it will all be written on one line. To prevent syntax errors, I would still suggest escaping all quotes. It's just a good habit.
5th Mar 2021, 2:05 PM
Ronald J. Tempel
Ronald J. Tempel - avatar
+ 2
"within HTML you can write commands wich are executed" ... and you could also write loops, define function, and so on. but you do it with JavaScript embeded inside HTML, not with it... without JS, you cannot "write commands wich are executed" with only HTML... so even if we take your definition of a programing language, HTML is definitively not one!
7th Mar 2021, 10:22 AM
visph
visph - avatar
+ 2
Ronald J. Tempel html tags are not commands, they are... tags ^^ a program (your browser) parse html and build the structure defined in it... but that program is not done with html. html is just a structured data format, not a programing language... would you say that png is a programming language? but png "execute commands" as let you able to display a picture... but the program wich do so is not the data itself ;) despite your logic, almost all people are agree that HTML is not a programming language but a markup language... being human readable/writable does not mean that's programing... you could say that's a coding language but not a programing one ;P
7th Mar 2021, 10:38 AM
visph
visph - avatar
+ 1
import os button = ''' <button>Button</button> ''' print(button) os.system('touch file.png')
7th Mar 2021, 4:58 AM
Mahesh Muttinti
Mahesh Muttinti - avatar
0
hi
7th Mar 2021, 12:13 PM
Clown 15
Clown 15 - avatar
0
Clown 15 this is not a messenger place here only post answer https://www.sololearn.com/discuss/1316935/?ref=app
7th Mar 2021, 3:37 PM
Anurag Kumar
Anurag Kumar - avatar
- 1
To all who mock me and tell me that I should do my homework around HTML, I say the following. I would argue that HTML is a programming language. Can you make loops in HTML? No. Can you create functions in HTML? No. Can you create if statements in HTML? No. Can you compile HTML? No. And so many just assume what they are told by others and declare that HTML is not a programming language. However, a programming is not defined by loops, functions, if statements or even if they can be compiled or not. A programming language is defined by the fact that it's a language which you can use to write a command in order to be executed by a computer. Within HTML you can write commands which are executed on the client's computer. Therefor I believe HTML to be a valid programming language. Anyone can say anything, it's the reasoning behind a statement that actually matters. EDIT: To those who still don't understand. HTML tags are commands that create a structure.
7th Mar 2021, 10:14 AM
Ronald J. Tempel
Ronald J. Tempel - avatar
- 4
Hi
6th Mar 2021, 5:45 PM
420 Original
420 Original - avatar
- 4
hi
6th Mar 2021, 8:46 PM
haker tunisi
haker tunisi - avatar