Skip to content

Commit da1c901

Browse files
committed
Version 1.2.0: Updated ORM, finished tutorial on Docs and Added Versioning Tags
1 parent 93fc08a commit da1c901

File tree

20 files changed

+801
-81
lines changed

20 files changed

+801
-81
lines changed

Lemon/create_lemon_app.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from Lemon.ui.buttons import Buttons
1515
from random import choice
1616
17+
from models.model import insert_fake_data
18+
1719
Root = Component(\"Lemon\", \"public/css/style.css\", \"public/js/script.js\")
1820
app = Server(static_dir=\"public\")
1921
@@ -40,6 +42,7 @@ def item(props: dict):
4042
4143
@app.route("/")
4244
def home(request, response):
45+
insert_fake_data()
4346
response.text = Root.render('<App/>')
4447
4548
app.run()
@@ -96,18 +99,19 @@ def home(request, response):
9699
97100
class model(baseModel):
98101
base_model = ClassBase
99-
table_name = "model"
102+
tablename = "model"
100103
fields = ("field1", "field2")
101104
102105
model_list = [model]
103106
104107
migrate(model_list).migrate()"""
105108

106-
sql_model = """from Lemon.orm import DBManager
109+
sql_model = """from Lemon.orm.DBManager import SqliteManager
107110
108-
sql = DBManager.SQLConnectionManager("../model.db")
111+
sql = SqliteManager("../model.db")
109112
# inserts some fake data to field1 and field2
110-
sql.insert("model", [("field1", "field2"), ("Hello", "World")])
113+
def insert_fake_date():
114+
sql.insert("model", [("field1", "field2"), ("Hello", "World")])
111115
"""
112116

113117
def main():
@@ -139,6 +143,8 @@ def main():
139143

140144
open("models/model.py", "w+", encoding="utf-8").write(sql_model)
141145

146+
open("models/__init__.py", "w+", encoding="utf-8").write("")
147+
142148
open("README.md", "w+", encoding="utf-8").write(readme_code)
143149

144150
print(f"App created @ /{args.app_name}/")

Lemon/orm/DBManager.py

Lines changed: 81 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
import sqlite3 as sql3
22
import datetime
33

4-
class SQLConnectionManager:
5-
"""SQL Connection Manager"""
64

5+
class SQLConnectionManager():
76
def __init__(self,filename):
8-
"""SQL Connection Manager: Init"""
97
self.filename = filename
10-
self.connection = sql3.connect(filename)
8+
self.connection = sql3.connect(self.filename)
119
self.cursor = self.connection.cursor()
1210

1311
def __enter__(self):
14-
"""when the class is entered as a context manager"""
1512
print("Connection started ...")
1613
self.connection = sql3.connect(self.filename)
1714
self.cursor = self.connection.cursor()
1815
return self
1916

2017
def commit(operation):
21-
"""Commit Operation"""
2218
def wrapper(self, tablename, fields):
2319
operation(self, tablename, fields)
2420
self.connection.commit()
@@ -27,7 +23,6 @@ def wrapper(self, tablename, fields):
2723

2824
@commit
2925
def create_table(self, tablename, fields):
30-
"""Create Table"""
3126
fields = list(fields)
3227
fields = " text, ".join(fields) +" text"
3328
drop_command = f"DROP TABLE IF EXISTS {tablename}"
@@ -40,98 +35,112 @@ def create_table(self, tablename, fields):
4035
finally:
4136
print(f"{tablename}: created successfully!")
4237

43-
@commit
44-
def insert(self, tablename, fields: list):
45-
"""Insert"""
46-
values = fields[1]
47-
fields = fields[0]
48-
fields = ", ".join(fields)
49-
values = ", ".join(values)
50-
insert_command = f"INSERT INTO {tablename} ({fields}) VALUES ({values})"
51-
try:
52-
self.cursor.execute(insert_command)
53-
except sql3.Error as er:
54-
print(f"SQLite error: {' '.join(er.args)}")
38+
def alter_table(self, ):
39+
pass
5540

56-
@commit
57-
def delete(self, tablename, fields):
58-
"""Delete"""
59-
delete_command = f"DELETE FROM {tablename} WHERE {fields[0]} = {fields[1]}"
60-
try:
61-
self.cursor.execute(delete_command)
62-
except sql3.Error as er:
63-
print(f"SQLite error: {' '.join(er.args)}")
41+
@property
42+
def show_tables(self):
43+
command = "SELECT * FROM sqlite_master WHERE type='table';"
44+
return self.cursor.execute(command)
6445

65-
@commit
66-
def update(self, tablename, fields):
67-
"""Update"""
68-
update_command = f"UPDATE {tablename} SET {fields[0]} = {fields[1]} WHERE {fields[2]} = {fields[3]}"
69-
try:
70-
self.cursor.execute(update_command)
71-
except sql3.Error as er:
72-
print(f"SQLite error: {' '.join(er.args)}")
46+
def __exit__(self, type, value, traceback):
47+
print("Connection ended ...")
48+
self.connection.close()
7349

74-
@commit
75-
def select(self, tablename, fields):
76-
"""Select"""
77-
select_command = f"SELECT {fields[0]} FROM {tablename} WHERE {fields[1]} = {fields[2]}"
78-
try:
79-
self.cursor.execute(select_command)
80-
return self.cursor.fetchall()
81-
except sql3.Error as er:
82-
print(f"SQLite error: {' '.join(er.args)}")
50+
51+
class SqliteManager():
52+
def __init__(self, filename):
53+
self.filename = filename
54+
self.connection = sql3.connect(self.filename)
55+
self.cursor = self.connection.cursor()
56+
57+
def __enter__(self):
58+
return self
59+
60+
def commit(operation):
61+
def wrapper(self, tablename, fields):
62+
operation(self, tablename, fields)
63+
self.connection.commit()
64+
print(f"{datetime.datetime.now()}: Commit is successful!!")
65+
return wrapper
8366

8467
@commit
85-
def drop_table(self, tablename, fields = None):
86-
"""Drop Table"""
68+
def create_table(self, tablename, fields):
69+
fields = list(fields)
70+
fields = " text, ".join(fields) +" text"
8771
drop_command = f"DROP TABLE IF EXISTS {tablename}"
72+
create_command = f"CREATE TABLE {tablename} ({fields})"
8873
try:
8974
self.cursor.execute(drop_command)
75+
self.cursor.execute(create_command)
9076
except sql3.Error as er:
9177
print(f"SQLite error: {' '.join(er.args)}")
78+
finally:
79+
print(f"{tablename}: created successfully!")
9280

9381
@commit
94-
def get_column(self, tablename, fields):
95-
"""Get Column"""
96-
get_column_command = f"SELECT {fields[0]} FROM {tablename}"
97-
try:
98-
self.cursor.execute(get_column_command)
99-
return self.cursor.fetchall()
100-
except sql3.Error as er:
101-
print(f"SQLite error: {' '.join(er.args)}")
82+
def insert(self, tablename, fields):
83+
fields = list(fields)
84+
print(fields)
85+
values = fields[1]
86+
fields = fields[0]
87+
fields = ", ".join(fields)
88+
values = ", ".join(values)
89+
print(fields, values)
90+
command = f"INSERT INTO {tablename} ({fields}) VALUES ({values})"
91+
print(command)
92+
self.cursor.execute(command)
10293

10394
@commit
104-
def get_table(self, tablename, fields = None):
105-
"""Get Table"""
106-
get_table_command = f"SELECT * FROM {tablename}"
107-
try:
108-
self.cursor.execute(get_table_command)
109-
return self.cursor.fetchall()
110-
except sql3.Error as er:
111-
print(f"SQLite error: {' '.join(er.args)}")
95+
def update(self, tablename, fields):
96+
fields = list(fields)
97+
values = fields[1]
98+
fields = fields[0]
99+
fields = ", ".join(fields)
100+
values = ", ".join(values)
101+
command = f"UPDATE {tablename} SET {fields} WHERE {values}"
102+
self.cursor.execute(command)
103+
104+
@commit
105+
def delete(self, tablename, fields):
106+
fields = list(fields)
107+
values = fields[1]
108+
fields = fields[0]
109+
fields = ", ".join(fields)
110+
values = ", ".join(values)
111+
command = f"DELETE FROM {tablename} WHERE {fields} = {values}"
112+
self.cursor.execute(command)
113+
114+
@commit
115+
def select(self, tablename, fields):
116+
fields = list(fields)
117+
values = fields[1]
118+
fields = fields[0]
119+
fields = ", ".join(fields)
120+
values = ", ".join(values)
121+
command = f"SELECT {fields} FROM {tablename} WHERE {values}"
122+
self.cursor.execute(command)
123+
return self.cursor.fetchall()
124+
125+
@commit
126+
def select_all(self, tablename):
127+
command = f"SELECT * FROM {tablename}"
128+
self.cursor.execute(command)
129+
return self.cursor.fetchall()
112130

113131
@property
114132
def show_tables(self):
115-
"""Show Tables"""
116133
command = "SELECT * FROM sqlite_master WHERE type='table';"
117134
return self.cursor.execute(command)
118135

119136
@property
120137
def show_columns(self):
121-
"""Show Columns"""
122-
command = "SELECT * FROM sqlite_master WHERE type='table';"
123-
return self.cursor.execute(command)
124-
125-
@property
126-
def show_rows(self):
127-
"""Show Rows"""
128138
command = "SELECT * FROM sqlite_master WHERE type='table';"
129139
return self.cursor.execute(command)
130140

131141
def __exit__(self, type, value, traceback):
132-
"""when the class is exited as a context manager"""
133-
print("Connection ended ...")
134142
self.connection.close()
143+
135144

136145
class base:
137146
"""Base Class"""
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
title: Hello World!
3+
sidebar_position: 3
4+
---
5+
6+
# Lemon 🍋: Hello-World!
7+
8+
Hello, in this part. we're going to build a small lemon app.
9+
10+
## Getting Started
11+
12+
first of, let's create a virtual environment.
13+
14+
```bash
15+
#with venv
16+
python -m venv venv
17+
18+
#with virtualenv
19+
python -m virtualenv venv
20+
```
21+
22+
then activate it with the below command.
23+
24+
```bash
25+
#windows
26+
.\\venv\\Scripts\\activate
27+
```
28+
29+
after that install Lemon in it.
30+
31+
```bash
32+
pip install Lemon-Library
33+
```
34+
35+
## creating the app
36+
37+
now in that app create a file called `app.py`
38+
39+
now in that app paste this code (don't worry I will explain the code).
40+
41+
```python
42+
from Lemon.components import Component
43+
from Lemon.Server.server import Server
44+
45+
app = Server(None)
46+
Root = Component("Home")
47+
48+
class home(Component):
49+
50+
name = "home"
51+
52+
def item(props: dict):
53+
return """
54+
<h1>Hello World!</h1>
55+
<p>This is generated in Python!</p>
56+
<p><strong>This is in BOLD</strong></p>
57+
<p><em>This is in ITALICS</em></p>
58+
<a href=\"https://github.com/Sas2k\">My Github Profile</a>
59+
"""
60+
61+
Root.add(home)
62+
63+
@app.route("/")
64+
def student(request, response):
65+
home_page = Root.render("<home/>")
66+
response.text = home_page
67+
68+
app.run()
69+
```
70+
71+
now to run it, just do this.
72+
73+
`python app.py`
74+
75+
## Code Break Down 🥚🍳
76+
77+
Now let's break the code down.
78+
79+
in the first 2 lines, it imports the server and the Component Class. (which is the base class to create the components.)
80+
81+
then we define an `app` variable, which is the server instance.
82+
the `root` variable is the root component where the components are rendered.
83+
84+
then there is the `Home` Class, which is a component.
85+
86+
in the component class, there is a `name` variable, which is the variable that says the name of the variable.
87+
88+
then the `item` function is where the components stuff is there (like the filling of a pie 🥧).
89+
90+
the `item` function has a `props` dictionary object. Which are the properties of the element.
91+
92+
For example: `<exampleComponent name="Sas2k"/>
93+
94+
here the `name="Sas2k"` is the prop.
95+
96+
After that the component is added to the Root with the `Root.add`.
97+
98+
then their is the usual route function.
99+
100+
After, their is the `app.run` which runs it.
101+
102+
And that's basically it.
103+
104+
_A Lemon app, broken down._

0 commit comments

Comments
 (0)