Python Compiler Using Django
Last Updated :
24 Apr, 2025
In this article, we will explore the creation of a Python compiler using Django. Users can input their Python code into the designated code area, and upon clicking the "Run" button, the compiler will generate the corresponding output.
What is Python Compiler?
A Python compiler is a program or tool that translates human-readable Python code into machine-readable bytecode or native machine code. Unlike an interpreter that executes code line by line, a compiler processes the entire code at once, generating an intermediate representation or executable file. Python compilers play a crucial role in transforming high-level Python code into a format that a computer's hardware can execute directly, making the code run.
Create Python Compiler Using Django
Below, is the step-by-step guide to creating a Python Compiler Using Django:
Starting the Project Folder
To start the project use this command
django-admin startproject core
cd core
To start the app use this command
python manage.py startapp home
Now add this app to the ‘settings.py’
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"home",
]
File Structure

Setting Necessary Files
views.py : below,code defines a Django view for executing Python code submitted through a web form. It uses `exec` to run the code, captures the output, and handles exceptions, providing detailed error information if any. The `index` function renders an HTML template, while `runcode` processes a POST request containing Python code, executes it, and updates the web page with the code and its output.
Python3
# views.py
from django.shortcuts import render
import traceback
from io import StringIO
from contextlib import redirect_stdout # Add this import
def execute_code(code):
try:
# Capture standard output in a buffer
output_buffer = StringIO()
with redirect_stdout(output_buffer):
exec(code)
output = output_buffer.getvalue()
except Exception as e:
# Provide detailed error information
output = f"Error: {str(e)}\n{traceback.format_exc()}"
return output
def index(request):
return render(request, 'index.html')
def runcode(request):
if request.method == "POST":
codeareadata = request.POST['codearea']
output = execute_code(codeareadata)
return render(request, 'index.html', {"code": codeareadata, "output": output})
return HttpResponse("Method not allowed", status=405)
core/urls.py : below code sets up URL patterns for a Django project. It includes the admin interface at '/admin/' and incorporates URLs from the 'home' app at the project's root ('/').
Python3
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('home.urls')),
]
home/urls.py : below Django URL configuration defines two patterns. The empty path maps to the 'index' view, identified as 'indexpage'. The 'runcode' path links to the 'runcode' view.
Python3
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index, name="indexpage"),
path('runcode', views.runcode, name="runcode"),
]
Creating GUI
home/templates/index.html : This HTML file creates a straightforward Python compiler web page with Django. It features a CodeMirror code area, a 'Run Code' button, and an output section. The design is clean with a green color scheme and a GeeksforGeeks-themed header. The button triggers code execution, and the output is displayed below the input area. The page maintains a responsive layout.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>GeeksforGeeks</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.62.2/codemirror.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.62.2/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.62.2/mode/python/python.min.js"></script>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f0f4f7;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}
form {
width: 100%;
max-width: 800px;
background-color: #ffffff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
}
label {
display: block;
margin-bottom: 12px;
font-weight: bold;
color: black;
font-family: 'Times New Roman', Times, serif;
}
#codearea,
#output {
height: 300px;
font-size: 16px;
width: 100%;
border: 1px solid green;
border-radius: 5px;
padding: 12px;
margin-bottom: 24px;
resize: vertical;
}
#output {
height: 150px;
padding: 5px;
}
button {
color: green;
border: none;
padding: 10px 20px;
border-radius: 5px;
border: 1px solid green;
cursor: pointer;
font-size: 15px;
}
button:hover {
background-color: #388e3c;
color: white;
}
</style>
</head>
<body>
<h2 style="font-family: 'Times New Roman', Times, serif; "> <strong style="color: green;">GeeksforGeeks</strong> Python Compiler</h2>
<form action="/runcode" method="post">
{% csrf_token %}
<div>
<label for="codearea">Code Area</label>
<textarea id="codearea" name="codearea" rows="10" >{{ code }}</textarea>
</div>
<hr>
<div>
<label for="output">Output</label>
<textarea id="output" name="output" rows="4" disabled>{{ output }}</textarea>
</div>
<button type="submit">Run Code</button>
</form>
<script>
var codeEditor = CodeMirror.fromTextArea(document.getElementById("codearea"), {
mode: "python",
lineNumbers: true,
indentUnit: 4,
extraKeys: { Tab: "indentMore", "Shift-Tab": "indentLess" },
});
</script>
</body>
</html>
Deployment of the Project
Run these commands to apply the migrations:
python3 manage.py makemigrations
python3 manage.py migrate
Run the server with the help of following command:
python3 manage.py runserver
Output

Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read