An open API service indexing awesome lists of open source software.

https://github.com/thakeenathees/localhost

A no setup lightweight python web framework -- for localhost
https://github.com/thakeenathees/localhost

bootstrap localhost python3 webframework

Last synced: 11 months ago
JSON representation

A no setup lightweight python web framework -- for localhost

Awesome Lists containing this project

README

          

# localhost
A no setup lightweight python web framework -- for localhost

## install
clone this repository to your python import path and run the below command

```
git clone https://github.com/ThakeeNathees/localhost.git
python localhost init .
python main.py
```
the command `python localhost init .` creates a python file main.py as below and a directory called server_data
```python
from localhost import Server
server = Server(port=8000)
server.run()
```
```
server_data
+-- __init__.py
+-- settings.py
+-- db/
| +-- auth
| +-- users.csv
| +-- sessions.csv
|
+-- static/
| +-- css/
| | +-- bootstrap.min.css
| | +-- main.css
| +-- html/
| +-- js/
| | +-- bootstrap.min.js
| | +-- jquery-3.2.1.slim.min.js
| | +-- popper.min.js
| +-- image/
|
+-- templates/
+-- localhost/
+-- admin/
| +-- home.html
| +-- login.html
| +-- logout.html
+-- base.html
+-- home.html
+-- error.html
```

now the server will run at `http://www.localhost:8000/` and you'll see the localhost default home page (for the admin page : localhost:8000/admin)

## screenshot



## url routing
To add your url paths
```python
from localhost import Server
from localhost.urls import path
from localhost.response import HttpResponse

server = Server(port=8000)

def home_view(request):
return HttpResponse('

welcome to the home page!!!

')

urlpatterns = [
path('/home', home_view, name='home-page')
]

server.urlpatterns = urlpatterns

server.run()
```

## render html
Instead of HttpResponse you can return a rendered html page.

in the settings.py the default path for the templates are specified as
```TEMPLATE_DIR = os.path.join(SERVER_DATA_DIR, 'templates')```

create your home-page template (home.html) in your TEMPLATE_DIR

```html
{{ html_base_begin }}


Welcome!!!


With localhost using "html_base_begin" and "html_base_end" context you can use boostrap,
and a simple html layout, all you have to write here is the body of the page.




{{ replace_me }}


{{ html_base_end }}
```
now render the template
```python
from localhost.response import render
def home_view(request):
return render(request, 'home.html', ctx={
'replace_me':'

you can use context to replace with html source code

'
})
```