Skip to main content

Create HTML pages using python and open automatically on execution.

This process of creation of HTML pages using python is similar to process of creation of any other file.
To automatically open the pages after execution, we will use selenium webdriver.



Following are the steps:

1. Open any text editor and create a python file with .py extension.

2. Import selenium packages. (Make sure you download them first.)

For downloading on Ubuntu, use these commands in terminal:
$ sudo apt-get install python-pip
$ sudo pip install selenium


Import:
from selenium import webdriver


3. Write the following to create HTML file on execution.

f = open('helloworld.html','w')


4. In order to write in this file, use the following code:



f.write("""<!DOCTYPE HTML>
<html> <header><title>This is title</title></header> <body> Hello world </body> </html>
""")
5. Close command :

f.close()

6. Use the following snippet to open the page automatically on execution:
Note: I have used Chrome. You can use Firefox if you want.

browser = webdriver.Chrome()
browser.get('file:PATH/helloworld.html')

Replace the PATH with the path of the folder where this program is being written.

That's all!

(Bonus):
Here you can add huge HTML codes. If you wish to add some custom strings from the program, use the following while writing the file:

f.write("""<!DOCTYPE HTML>
<html>
<header><title>This is title</title></header>
<body>
Hello world
<h3>{0}</h3>
<h2>{1}</h2>
</body>
</html>
""".format(str1,str2))
where str1 and str2 are 2 strings whose value will be written in {0} and {1} respectively.




Although this method is simple, this is not the correct way of designing HTML sites using python. Therefore, once created, there is no python interaction with the webpage. You may use other automation tools in python to improve its functionality.

Comments

Popular posts from this blog

Create drop-down menu with image icons in Android

Hello devs! In this tutorial, we will be creating a drop-down menu on clicking a button in Android studio. Here is an image of how our menu is going to look: On clicking the hamburger icon, a vertical menu will appear with image icons of your choice. Above image is just to get an idea of what we are going to develop. Let's get started. Step 1: Open the layout file where you want the menu. Add the hamburger icon on an image button. <ImageButton android :layout_width= "50dp" android :layout_height= "50dp" android :src= "@drawable/ham" android :id= "@+id/ham" android :background= "#00000000" />  Note : #00000000 specifies 'transparent' background. Step 2: Add a vertical linear layout for the menu. Inside that, add nested linear layouts for horizontal rows. <LinearLayout android :layout_width= "wrap_content" android :layout_height= "wrap_content...

Shake a view(edit text) on incorrect input at OTP,passwords,etc (Android)

Hello devs! This is another short tutorial to show how to shake an Edit text view if the input is incorrect. Note : The tutorial assumes that you have the basic knowledge about android development. It involves the following steps : 1. Create the Edit text and cast it inside the onCreate. EditText edittext=findViewById(R.id.edit_text); Recommended : https://github.com/GoodieBag/Pinview Optional - Put the edit text inside a dialog to take an input from the dialog. 2. Write an animation XML. Here is an example: ( shake.xml ) Courtesy : https://gist.github.com/simon-heinen/9795036 <? xml version = " 1.0 " encoding = " utf-8 " ?> < set xmlns : android = " http://schemas.android.com/apk/res/android " > < rotate android : duration = " 70 " android : fromDegrees = " -5 " androi...

Fix 'UNEXPECTED INCONSISTENCY; Run fsck manually' on Ubuntu

If you have the following Ubuntu error, I might have a fix for it. fsck from util-linux 2.26.2 /dev/sda1 contains a file system with errors, check forced. /dev/sda1: Inodes that were part of a corrupted orphan linked list found. /dev/sda1: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.         (i.e., without -a or -p options) fsck exited with status code 4 The root filesystem on /dev/sda1 requires a manual fsck Busybox v1.22.1 (Ubuntu 1:1.22.0-15ubuntu1) built in shell (ash) Enter 'help' for a list of built-in commands. (initramfs) _ And believe me help command does nothing. Solution: Just fire a simple command to execute  fsck manually. fsck /dev/sda1 If some other filesystem is causing the error then mention that name after fsck. It will show some processing. If it asks any questions like Fix(y/n)? always enter y . In the end when (initramfs) reappears enter: reboot Well, that's all! It worked for me. Hope it works for ...