+ 1

How to add python to html document?

19th Dec 2017, 2:35 AM
Benarjee Sambangi
Benarjee Sambangi - avatar
1 Answer
+ 6
”Hello World” in HTML using Python One of the more powerful ideas in computer science is that a file that seems to contain code from one perspective can be seen as data from another. It is possible, in other words, to write programs that manipulate other programs. What we’re going to do next is create an HTML file that says “Hello World!” using Python. We will do this by storing HTML tags in a multiline Python string and saving the contents to a new file. This file will be saved with an .htmlextension rather than a .txtextension. Typically an HTML file begins with a doctype declaration. You saw this when you wrote an HTML “Hello World“. To make reading our code easier, we will omit the doctype in this example. Recall a multi-line string is created by enclosing the text in three quotation marks (see below). # write-html.py f = open('helloworld.html','w') message = """<html> <head></head> <body><p>Hello World!</p></body> </html>""" f.write(message) f.close() Save the above program as write-html.py and execute it. Use File -> Open in your chosen text editor to open helloworld.html to verify that your program actually created the file. The content should look like this: Now go to your Firefox browser and choose File -> New Tab, go to the tab, and choose File -> Open File. Select helloworld.html. You should now be able to see your message in the browser. Take a moment to think about this: you now have the ability to write a program which can automatically create a webpage. There is no reason why you could not write a program to automatically create a whole website if you wanted to. Or you can also use the library https://www.djangoproject.com
19th Dec 2017, 2:43 AM
James16
James16 - avatar