Skip to content

Commit 672d57b

Browse files
committed
Added Django example
1 parent a1c74fe commit 672d57b

34 files changed

+506
-0
lines changed

examples/django/frontend/css/bootstrap.min.css

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
body{
2+
background: #eeeeee;
3+
}
4+
5+
.todo{
6+
padding: 30px 0;
7+
}
8+
9+
.todo-card{
10+
border-radius: 4px;
11+
background: #fff;
12+
height: 100%;
13+
padding: 20px;
14+
}
15+
16+
17+
.todo-box{
18+
padding: 10px;
19+
}
20+
21+
.todo-box h6{
22+
padding: 30px;
23+
border: 2px solid #000;
24+
margin-top: 5px;
25+
}
26+
27+
.todo-box .btn-delete{
28+
padding: 10px;
29+
width: 100px;
30+
height: 80px;
31+
margin-top: 5px;
32+
}
33+
34+
.todo-box .btn-add{
35+
padding: 10px;
36+
width: 100px;
37+
height: 50px;
38+
margin-top: 4px;
39+
40+
}
41+

examples/django/frontend/index.html

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>Pyscript Todo</title>
7+
<link rel="stylesheet" href="css/bootstrap.min.css">
8+
<link rel="stylesheet" href="css/style.css">
9+
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
10+
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
11+
12+
13+
</head>
14+
<body>
15+
<py-env>
16+
- autoclose_loader: true
17+
- runtimes:
18+
-
19+
src: "/pyodide.js"
20+
name: pyodide-0.20
21+
lang: python
22+
- paths:
23+
- script/app.py
24+
</py-env>
25+
<section class="todo">
26+
<div class="container">
27+
<div class="row justify-content-center mt-5">
28+
<div class="col-lg-8 text-center">
29+
<h2 class="lead">Pyscript App</h2>
30+
</div>
31+
32+
<div class="col-lg-6 mt-5">
33+
<div class="todo-card">
34+
<div class="t-line">
35+
<div class="todo-box" >
36+
<div class="row" id="todo-row">
37+
<div class="form-group">
38+
<input type="text" name="addtodo" id="taskadd" class="form-control">
39+
<button pys-onClick="create" id="add" type="button" class="btn shadow-none mt-2 btn-outline-light btn-add btn-primary" >Add</button>
40+
</div>
41+
42+
</div>
43+
</div>
44+
</div>
45+
46+
</div>
47+
</div>
48+
49+
</div>
50+
</div>
51+
</section>
52+
<py-script>
53+
from app import GetTasks,delete,create
54+
from pyodide import create_proxy
55+
56+
</py-script>
57+
58+
</body>
59+
</html>
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import json
2+
import asyncio
3+
from pyodide.http import pyfetch
4+
from pyodide import JsException,create_proxy
5+
import js
6+
7+
8+
9+
async def GetTasks():
10+
response = await pyfetch(
11+
url="http://127.0.0.1:8000/",
12+
method="GET",
13+
headers={"Content-Type": "application/json"},
14+
)
15+
if response.ok:
16+
data = await response.json()
17+
parent = js.document.querySelector("#todo-row")
18+
js.document.querySelector('#taskadd').value =""
19+
before_child = js.document.querySelectorAll(".task-test")
20+
before_child2 = js.document.querySelectorAll("#del")
21+
if before_child and before_child2:
22+
for b in before_child:
23+
b.remove()
24+
for c in before_child2:
25+
c.remove()
26+
i=0
27+
for t in data:
28+
i +=1
29+
html_data =js.document.createElement("h6")
30+
html_data.className = "task-test col-8"
31+
html_data.innerHTML = t["task"]
32+
parent.appendChild(html_data)
33+
button=js.document.createElement("button")
34+
button.className = "btn btn-delete btn-outline-light btn-danger col-4"
35+
button.innerHTML = "Delete"
36+
button.value = t["id"]
37+
button.setAttribute("id", "del")
38+
button.addEventListener("click", create_proxy(delete))
39+
parent.appendChild(button)
40+
41+
42+
43+
44+
45+
async def create(e):
46+
task = js.document.querySelector('#taskadd').value
47+
response = await pyfetch(
48+
url=f"http://127.0.0.1:8000/",
49+
method="POST",
50+
headers={"Content-Type": "application/json"},
51+
body = json.dumps({
52+
"task":task
53+
})
54+
)
55+
loop = asyncio.get_event_loop()
56+
loop.run_until_complete(GetTasks())
57+
58+
59+
async def delete(e):
60+
id = e.target.value
61+
response = await pyfetch(
62+
url=f"http://127.0.0.1:8000/delete/{id}",
63+
method="DELETE",
64+
headers={"Content-Type": "application/json"},
65+
66+
)
67+
loop = asyncio.get_event_loop()
68+
loop.run_until_complete(GetTasks())
69+
70+
71+
72+
loop = asyncio.get_event_loop()
73+
loop.run_until_complete(GetTasks())

examples/django/todo/app/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

examples/django/todo/app/admin.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.contrib import admin
2+
from .models import Todo
3+
# Register your models here.
4+
5+
admin.site.register(Todo)

examples/django/todo/app/apps.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AppConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'app'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Generated by Django 4.0.5 on 2022-06-30 19:20
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
initial = True
9+
10+
dependencies = [
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Todo',
16+
fields=[
17+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('task', models.CharField(max_length=500)),
19+
('created_on', models.DateTimeField(auto_now_add=True)),
20+
],
21+
),
22+
]

examples/django/todo/app/migrations/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.

examples/django/todo/app/models.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
from django.db import models
3+
4+
# Create your models here.
5+
6+
class Todo(models.Model):
7+
task = models.CharField(max_length=500)
8+
created_on = models.DateTimeField(auto_now_add=True)
9+
10+
def __str__(self):
11+
return self.task
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
from pyexpat import model
3+
from rest_framework import serializers
4+
from .models import Todo
5+
6+
7+
8+
class TodoSerializer(serializers.ModelSerializer):
9+
class Meta:
10+
model = Todo
11+
fields = '__all__'

examples/django/todo/app/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

examples/django/todo/app/urls.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.urls import path, include
2+
from . import views
3+
4+
urlpatterns = [
5+
path('', views.GetTodo),
6+
path('delete/<int:id>', views.DeleteTodo),
7+
]

examples/django/todo/app/views.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from rest_framework.decorators import api_view
2+
from rest_framework.response import Response
3+
from django.http import HttpResponse
4+
from .serializers import TodoSerializer
5+
from .models import Todo
6+
# Create your views here.
7+
@api_view(["GET", "POST"])
8+
def GetTodo(request):
9+
if request.method == "GET":
10+
try:
11+
todo = Todo.objects.all()
12+
except:
13+
return HttpResponse(status = 404)
14+
serializer = TodoSerializer(todo, many=True)
15+
return Response(serializer.data)
16+
if request.method == "POST":
17+
print(request.data)
18+
serializer = TodoSerializer(data=request.data)
19+
if serializer.is_valid():
20+
serializer.save()
21+
return Response(serializer.data)
22+
return HttpResponse(status=201)
23+
24+
@api_view(["DELETE"])
25+
def DeleteTodo(request,id):
26+
if request.method == "DELETE":
27+
try:
28+
todo = Todo.objects.get(pk=id)
29+
except:
30+
return HttpResponse(status = 404)
31+
data ={}
32+
operation = todo.delete()
33+
if operation:
34+
data["success"] = "deleted"
35+
return Response(data)
36+
else:
37+
data["success"] = "error"
38+

examples/django/todo/db.sqlite3

132 KB
Binary file not shown.

examples/django/todo/manage.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
"""Run administrative tasks."""
9+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo.settings')
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
19+
20+
21+
if __name__ == '__main__':
22+
main()

examples/django/todo/todo/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

examples/django/todo/todo/asgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for todo project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo.settings')
15+
16+
application = get_asgi_application()

0 commit comments

Comments
 (0)