Python Pyramid - Static Assets
Last Updated :
24 Apr, 2025
A lightweight web framework in Python that converts small web apps to large web apps is called Pyramid. There are various circumstances when the user needs to add some images, HTML code, CSS code, etc. along with the Python code in the web app. These are called static assets and can be added using the Pyramid in Python.
Python Pyramid - Static Assets
Syntax: config.add_static_view(name='static',path='static')
Parameters:
- name='static' : name or route prefix for the static view
- path='static' : directory on the server where the static files are stored
The 'static' directory is used to store the static assets. There are three static assets in Python Pyramid:
Static Asset: Image
The visual representation of something is called an image. It is very crucial to add images to the app to make it attractive. In this way, we will see how we can add static assets, and images to the web app using Pyramid.
app2.py
In this example, we created a Python file app2.py under the package pyramid1. We have defined a constructor in the app in which we have added the static view.
Python3
# Import wsgiref and pyramid libraries
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.view import view_config
# Create view config
@view_config(
route_name='hello',
renderer="static/templates/home.jinja2"
)
# Create a function to print value
def home(request):
return {'greet':'Welcome to Geeks For Geeks!'}
# Create a constructor to define the app
if __name__=="__main__":
with Configurator() as config:
config.include('pyramid_jinja2')
config.add_static_view(name='static',path='static')
config.add_route('hello','/')
config.scan()
app=config.make_wsgi_app()
server=make_server('0.0.0.0',6543,app)
server.serve_forever()
home.jinja2
Now, we have created a folder templates under static. In this templates folder, we have created the home.jinja2 file. In this file, we have defined the static asset image that we want to display on our web app.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{greet}}</title>
</head>
<body>
<h1> {{greet}}</h1>
<img src="static/geeksforgeeks.jpg" style="width: 570px;">
</body>
</html>
geeksforgeeks.png
Here, we have used the Geeks For Geeks logo (link) as an image, which we have placed in the static folder. Thus, the package will look similar to this:

Output:
Now, run the following command in the terminal to run the app.
python app2.py
For seeing the webapp, open the browser of your choice, and type the URL, http://localhost:6543/
-660.png)
Static Asset: CSS File
The way of displaying HTML elements in known as CSS. It is very crucial to add CSS in web app for making it attractive. In this way, we will see how we can add static asset: CSS to the web app using Pyramid.
app2.py
In this example, we created a Python file app2.py under the package pyramid1. We have defined a constructor in the app in which we have added the static view.
Python3
# Import wsgiref and pyramid libraries
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.view import view_config
# Create view config
@view_config(
route_name='hello',
renderer="static/templates/home.jinja2"
)
# Create a function to print value
def home(request):
return {'greet':'Welcome to Geeks For Geeks!'}
# Create a constructor to define the app
if __name__=="__main__":
with Configurator() as config:
config.include('pyramid_jinja2')
config.add_static_view(name='static',path='static')
config.add_route('hello','/')
config.scan()
app=config.make_wsgi_app()
server=make_server('0.0.0.0',6543,app)
server.serve_forever()
home.jinja2
Now, we have created a folder templates under static. In this templates folder, we have created the home.jinja2 file. In this file, we have defined the static asset CSS that we want to display on our web app.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{greet}}</title>
<link rel="stylesheet" href="static/style1.css"/>
</head>
<body>
<h1> {{greet}}</h1>
</body>
</html>
style1.css
Here, we have created the CSS file style1.css in the static folder.
CSS
*{
margin:0;
padding:0;
box:sizing:border-box;
background-color:green;
color:red;
font-family:sans-ser;
}
The package will look similar to this:

Output:
Now, run the following command in the terminal to run the app.
python app2.py
For seeing the webapp, open the browser of your choice, and type the URL, http://localhost:6543/
-660.png)
Static Asset: Javascript File
Javascipt is the most popular light-weighted programming language. It helps in making the web app reponsive. In this way, we will see how we can add static asset: javascript to the web app using Pyramid.
app2.py
In this example, we created a Python file app2.py under the package pyramid1. We have defined a constructor in the app in which we have added the static view.
Python3
# Import wsgiref and pyramid libraries
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.view import view_config
# Create view config
@view_config(
route_name='hello',
renderer="static/templates/home.jinja2"
)
# Create a function to print value
def home(request):
return {'greet':'Welcome to Geeks For Geeks!'}
# Create a constructor to define the app
if __name__=="__main__":
with Configurator() as config:
config.include('pyramid_jinja2')
config.add_static_view(name='static',path='static')
config.add_route('hello','/')
config.scan()
app=config.make_wsgi_app()
server=make_server('0.0.0.0',6543,app)
server.serve_forever()
home.jinja2
Now, we have created a folder templates under static. In this templates folder, we have created the home.jinja2 file. In this file, we have defined the static asset Javscript that we want to display on our web app.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{greet}}</title>
<script type = "text/javascript" src="static/function.js"></script>
</head>
<body>
<h1> {{greet}}</h1>
<input type = "button" onclick = "myfunction()" value = "Display">
</body>
</html>
function.js
Here, we have created the Javascript file function.js in the static folder.
JavaScript
function myfunction()
{
document.write("Hi, I am here!");
}
The package will look similar to this:

Output:
Now, run the following command in the terminal to run the app.
python app2.py
For seeing the webapp, open the browser of your choice, and type the URL, http://localhost:6543/

Conclusion
The static assets are used to make the web-app look attractive and reponsive. I hope after reading the above article, you will able to use the static assets in your Pyramid web app.
Similar Reads
Class method vs Static method in Python
In this article, we will cover the basic difference between the class method vs Static method in Python and when to use the class method and static method in python.What is Class Method in Python? The @classmethod decorator is a built-in function decorator that is an expression that gets evaluated a
5 min read
How to Handle Static Assets in Vite?
In Vite, static assets are files such as images, fonts, and other resources that are served directly to the browser. They should be placed in the public directory, which makes them accessible via the root URL. Vite efficiently handles these assets during development and builds them into the final ou
3 min read
Class or Static Variables in Python
All objects share class or static variables. An instance or non-static variables are different for different objects (every object has a copy). For example, let a Computer Science Student be represented by a class CSStudent. The class may have a static variable whose value is "cse" for all objects.
9 min read
Python Plotly - Exporting to Static Images
In this article, we will discuss how to export plotly graphs as static images using Python. To get the job done there are certain additional installations that need to be done. Apart from plotly, orca and psutil have to be installed. psutil (python system and process utilities) is a cross-platform P
2 min read
What is setup.py in Python?
Introduction In Python, setup.py is a module used to build and distribute Python packages. It typically contains information about the package, such as its name, version, and dependencies, as well as instructions for building and installing the package. This information is used by the pip tool, whic
3 min read
Reading images in Python
Python provides simple tools to work with images. Whether you're looking to open, view or save images there are many libraries to help. Let's see how to process the images using different libraries.1. Using ImageIO ImageIO is used for reading and writing images in various formats like PNG, JPEG, GIF
2 min read
Python staticmethod() Function
Python staticmethod() function is used to convert a function to a static function. Static methods are independent of class instances, meaning they can be called on the class itself without requiring an object of the class.Example:Pythonclass Utility: def msg(name): return f"Hello, {name}!" msg = sta
6 min read
Python PIL | Image.save() method
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files,
3 min read
Create an empty file using Python
File handling is a very important concept for any programmer. It can be used for creating, deleting, and moving files, or to store application data, user configurations, videos, images, etc. Python too supports file handling and allows users to handle files i.e., to read and write files, along with
3 min read
Python | Display images with PyGame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, itâs up to the imagination or necessity of the developer, what type of game he/she wants to develop usin
2 min read