Skip to content

Commit

Permalink
first version of my crud appication using flask
Browse files Browse the repository at this point in the history
  • Loading branch information
Awambeng committed Nov 8, 2023
0 parents commit a1c3601
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flask-crud-venv
__pycache__
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Flask CRUD

A simple Flask application for managing a list of books, demonstrating Create, Read, Update, and Delete (CRUD) operations.

## Features

- Add new books
- View a list of books
- Update a book's info
- Delete books


## Getting Started

1. Clone the repository:

```bash
git clone http://adorsys-share.local:3000/TheKid999/GIS-Trainees-Flask-CRUD.git
cd GIS-Trainees-Flask-CRUD
```

2. Set up a virtual environment and activate it:

```bash
python3 -m venv venv
source venv/bin/activate
```

3. Install the required dependencies:

```bash
pip install -r requirements.txt
```

4. Run the Flask application:

```bash
python app.py
```

5. Open a web browser and navigate to [http://127.0.0.1:8080/](http://127.0.0.1:8080/) to access the application.


## Contributing

1. Each group clones the repository to their local machine.
2. Students in each group create a new branch for the specific feature they are working on (e.g., `feature-add-book`, `feature-update-book`, etc.).
67 changes: 67 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from flask import Flask, render_template, redirect, request
from flask_sqlalchemy import SQLAlchemy
import pandas as pd

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///books.db'
db = SQLAlchemy(app)

class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
author = db.Column(db.String(100))
year = db.Column(db.Integer)


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///books.db'
db = SQLAlchemy(app)

class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
author =db.Column(db.String(100))
year = db.Column(db.Integer)


@app.route('/')
def index():
books = Book.query.all()
return render_template('index.html', books=books)


@app.route('/add', methods=['GET', 'POST'])
def add_book():
if request.method == 'POST':
new_book = Book(title=request.form['title'], author=request.form['author'], year=request.form['year'])
db.session.add(new_book)
db.session.commit()
return redirect('/')
return render_template('add_book.html')


@app.route('/edit_book/<int:book_id>', methods=['GET', 'POST'])
def edit_book(book_id):
book = Book.query.get(book_id)

if request.method == 'POST':
# Update the book details based on the form data
book.title = request.form['title']
book.author = request.form['author']
book.year = int(request.form['year'])
db.session.commit()
return redirect('/')
return render_template('edit_book.html', book=book)


@app.route('/delete/<int:id>')
def delete_book(id):
book_to_delete = Book.query.get_or_404(id)
db.session.delete(book_to_delete)
db.session.commit()
return redirect('/')

if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)


Binary file added books.db
Binary file not shown.
5 changes: 5 additions & 0 deletions data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Title,Author,Year
The Great Gatsby,F. scott Fitzgerald,1925
1984,George Orwell,1949
To Kill a Mockingbird,Harper Lee,1960

Binary file added instance/books.db
Binary file not shown.
24 changes: 24 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
blinker==1.7.0
click==8.1.7
Flask==3.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
Werkzeug==3.0.1
blinker==1.7.0
click==8.1.7
Flask==3.0.0
Flask-SQLAlchemy==3.1.1
greenlet==3.0.1
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
numpy==1.26.1
pandas==2.1.2
python-dateutil==2.8.2
pytz==2023.3.post1
six==1.16.0
SQLAlchemy==2.0.23
typing_extensions==4.8.0
tzdata==2023.3
Werkzeug==3.0.1
65 changes: 65 additions & 0 deletions templates/add_book.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html>
<head>
<title>Add Book</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

h1 {
color: #333;
order: -1;
margin-bottom: 20px;
}

form {
width: 300px;
display: flex;
flex-direction: column;
}

input[type="text"],
input[type="number"] {
padding: 10px;
margin-bottom: 10px;
width: 100%;
}

button[type="submit"] {
padding: 10px 20px;
background-color: #333;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;

}

button[type="submit"]:hover {
background-color: #555;
}

@media (max-width: 600px) {
form {
width: 80%;
}
}
</style>
</head>
<body>

<form method="post" action="/add">
<h1>Add Book</h1>
<input type="text" name="title" placeholder="Title" required><br>
<input type="text" name="author" placeholder="Author" required><br>
<input type="number" name="year" placeholder="Year" required><br>
<button type="submit">Add Book</button>
</form>
</body>
</html>

59 changes: 59 additions & 0 deletions templates/edit_book.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html>
<head>
<title>Edit Book</title>
<style>
body {
font-family: Arial, sans-serif;
}

h1 {
color: #333;
}

form {
margin-top: 20px;
}

label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}

input[type="text"],
input[type="number"] {
padding: 10px;
margin-bottom: 10px;
width: 300px;
}

button[type="submit"] {
padding: 10px 20px;
background-color: #333;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}

button[type="submit"]:hover {
background-color: #555;
}
</style>
</head>
<body>
<h1>Edit Book</h1>
<form action="/edit_book/{{ book.id }}" method="POST">
<label for="title">Title:</label>
<input type="text" name="title" id="title" value="{{ book.title }}" required>

<label for="author">Author:</label>
<input type="text" name="author" id="author" value="{{ book.author }}" required>

<label for="year">Year:</label>
<input type="number" name="year" id="year" value="{{ book.year }}" required><br>
<button type="submit">Update Book</button>
</form>
</body>
</html>
121 changes: 121 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<!--
<!DOCTYPE html>
<html>
<head>
<title>Book List</title>
</head>
<body>
<h1>Book List</h1>
<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
<th>Year</th>
</tr>
{% for book in books %}
<tr>
<td>{{ book.title }}</td>
<td>{{ book.author }}</td>
<td>{{ book.year }}</td>
<td><a href="/edit_book/{{ book.id }}">Edit</a> </td>
<td><a href="/delete/{{ book.id }}">Delete</a> </td>
</tr>
{% endfor %}
</table>
<a href="/add" class="add-book-link">Add a book</a>
</body>
</html> -->

<!DOCTYPE html>
<html>
<head>
<title>Book List</title>
<style>
body {
font-family: Arial, sans-serif;
}

h1 {
color: #333;
}

table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}

th, td {
padding: 10px;
text-align: left;
}

th {
background-color: #333;
color: #fff;
}

tr:nth-child(even) {
background-color: #f2f2f2;
}

a {
color: #333;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

.add-book-link {
display: block;
margin-top: 20px;
text-align: center;
color: green;
}

.delete-button {
background-color: red;
color: #fff;
border: none;
border-radius: 3px;
padding: 5px 10px;
cursor: pointer;
}

.update-button {
background-color: green;
color: #fff;
border: none;
border-radius: 3px;
padding: 5px 10px;
cursor: pointer;
}
</style>

</head>
<body>
<h1>Book List</h1>
<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
<th>Year</th>
</tr>
{% for book in books %}
<tr>
<td>{{ book.title }}</td>
<td>{{ book.author }}</td>
<td>{{ book.year }}</td>
<td><a href="/edit_book/{{ book.id }}" class="update-button">Edit</a></td>
<td><a href="/delete/{{ book.id }}" class="delete-button">Delete</a></td>
</tr>
{% endfor %}
</table>
<a href="/add" class="add-book-link">Add a book</a>
</body>
</html>


0 comments on commit a1c3601

Please sign in to comment.