Create Basic Webpage with Pyscript
Last Updated :
17 Oct, 2022
In this article, we will cover how we can create a basic webpage with Pyscript
The Pyscript is a python framework/library that allows us to write python code inside HTML directly with the help[ of pyscript tag. Pyscript is a new project and is currently under development, but it has gained a lot of attention recently. It can make a page generate dynamically with the help of python. We will see the basic web page created with the pyscript library in this article.
Creating a Template
We’ll create a basic template in HTML in which we will further add the pyscript framework as a link and a script to the pyscript CDN. You can create an index.html in a folder in your desired location.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title >PyScript Demo</ title >
</ head >
< body >
</ body >
</ html >
|
A simple HTML template can be created as per your preferences, we’ll leave it empty body right now to start afresh instance of the pyscript code.
Embedding Python inside HTML
We first need the script files that help us in allowing the runtime of python from the pyscript CDN. So, we will have to add a few link tags and the script tag that will bring in the Python Pyodide runtime.
<link rel=”stylesheet” href=”https://pyscript.net/alpha/pyscript.css” />
<script defer src=”https://pyscript.net/alpha/pyscript.js”></script>
The first link for the stylesheet will allow us to format the output generated from the python code. The next link which is a script tag will bring in the javascript source file that will work with the browser for interacting with the python runtime. Finally, we can use the pyscript tags for embedding the python code.
The python code can be embedded by adding inside the <py-script> tags. We can use the <py-script> tags to write python code. Normal python code can be embedded inside the <py-script> tags. The tags are not self-closing and hence like other HTML tags it needs a closing tag as </py-script>
<py-script>
#python code
</py-script>
The python code inside the py-script tags should be indented, as you write the python code. The formatting of the indentation is not strictly from the start of the line, though it needs to be consistent throughout while being inside the tags.
Example 1:
Here, we have used a simple python code for printing a variable name. The print function is parsed inside the pyscript that will in turn process the python and display the result as a native HTML tag. Though output from pyscript tags is native HTML, the generation is done with the help of Pyodide and WASM which generates the native HTML from the pyscript tags.
The webpage takes a few seconds to load in its entirety of the webpage. That’s because the python code has to be rendered and converted to a native HTML.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title >PyScript Demo</ title >
</ head >
< body >
< h1 >Hello,
< py-script >
name = "Geeks"
print(name)
</ py-script >
</ h1 >
</ body >
</ html >
|
Output:
Creating and Displaying List in Pyscript using loop
We can generate a list of elements in the HTML using a pure python list. We can simply use python syntax to print the elements of a list. The output will be generated as a distinct line which is the default behavior of the print statement in pyscript.
Example 2:
We can see the elements of the list are printed on the webpage using simple python code in the embedded pyscript tag.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title >PyScript Demo</ title >
</ head >
< body >
< py-script >
sports = ["Football", "Cricket", "Hockey", "Basketball"]
for sport in sports:
print(f" - {sport}")
</ py-script >
</ body >
</ html >
|
Output:
Creating and Displaying a dictionary using a conditional statement
To create a dictionary in python, we will be creating a simple dictionary and will display the key-value pairs one by one using a simple for loop.
Example 3:
Here, we have created a string and will further create a frequency map of each letter in the string. To create a frequency map of letters in the string, we will initialize an empty dictionary, we will iterate over the string, and check if the character in the string is present as a key in a dictionary or not, if it is present we will increment the value of the key as the character in the string, else the key is initialized to one.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title >PyScript Demo</ title >
</ head >
< body >
< py-script >
name = "GeeksforGeeks"
freq = {}
for s in name:
key = freq.keys()
if s in key:
freq[s] += 1
else:
freq[s] = 1
for k in freq:
print(f"{k} : {freq[k]}")
</ py-script >
</ body >
</ html >
|
Output:
So, we can see the dictionary has the key values as the character of the strings and the frequency of that character in the string.
Use the HTTP server to render the pyscript page
We can even use the HTTP server to render the pyscript page. We can do that using the http.server module to render the HTML page.
python -m http.server
Note – You need to be in the same directory as your index.html file else you need to specify the directory for rendering the HTML file.
python -m http.server -d 'path-to-directory'
The d flag is provided to locate the file in the directory without actually changing the directory in the terminal to run the HTTP server. After running the server, you need to locate the 127.0.0.1:8000 or localhost:8000 for viewing your HTML file.
Similar Reads
How to create a simple CGI script?
In this article, we will explore how to create a simple CGI (Common Gateway Interface) script on a Windows machine but before that, we need some basic ideas about these technologies ( CGI Script, HTTP, Web Server ).CGI Script: A CGI script is a program that runs on a web server and generates dynamic
2 min read
Interacting with Webpage - Selenium Python
Seleniumâs Python module is designed for automating web testing tasks in Python. It provides a straightforward API through Selenium WebDriver, allowing you to write functional and acceptance tests. To open a webpage, you can use the get() method for navigation. However, the true power of Selenium li
3 min read
Create GUI to Web Scrape articles in Python
Prerequisite- GUI Application using Tkinter In this article, we are going to write scripts to extract information from the article in the given URL. Information like Title, Meta information, Articles Description, etc., will be extracted. We are going to use Goose Module. Goose module helps to extrac
2 min read
Is Bash Script Better Than Python
Programming languages let you create programs and various online solutions like web apps, games, mobile apps, etc. There are multiple computer languages online that you can use to automate, assemble, interpret, and manage data processing. Programming languages like Bash and Python are prevalent amon
6 min read
Scraping websites with Newspaper3k in Python
Web Scraping is a powerful tool to gather information from a website. To scrape multiple URLs, we can use a Python library called Newspaper3k. The Newspaper3k package is a Python library used for Web Scraping articles, It is built on top of requests and for parsing lxml. This module is a modified an
2 min read
How to generate webpages using CGI scripts?
In this article, we will explore the process of generating webpages using CGI scripts. Furthermore, we will discuss the importance of CGI script implementation in facilitating dynamic content creation. Additionally, we'll delve into the intricacies of linking one page to another by creating multiple
4 min read
Web crawling with Python
Web crawling is widely used technique to collect data from other websites. It works by visiting web pages, following links and gathering useful information like text, images, or tables. Python has various libraries and frameworks that support web crawling. In this article we will see about web crawl
4 min read
Create a Website Alarm Using Python
We use Bookmarks to remember the important websites which we think we will use them very often in future. Here's a simple python code which takes the URL of the website as its first input and the time you want to open it as the second input. As the time reaches your given value of the time, it'll op
2 min read
How to Scrape Websites with Beautifulsoup and Python ?
Have you ever wondered how much data is created on the internet every day, and what if you want to work with those data? Unfortunately, this data is not properly organized like some CSV or JSON file but fortunately, we can use web scraping to scrape the data from the internet and can use it accordin
10 min read
How to generate basic HTML output using CGI scripts?
In this article, we will explore the process of generating basic HTML output using CGI scripts. Specifically, we will focus on creating a simple HTML web page using Python. The objective is to provide a step-by-step walkthrough, detailing the entire process of creating and utilizing CGI scripts to g
3 min read