In this tutorial, we will be using DADS5001, a NoSQL database called MongoDB, and a Python GUI library called PySimpleGUI to create a simple application to interact with the database. The tutorial will be led by Witsarut Wongsim, a talented software developer with a passion for learning and teaching.
To get started, make sure you have Python installed on your machine. You can download the latest version of Python from the official website (https://www.python.org/downloads/).
Next, we need to install MongoDB. MongoDB is a popular NoSQL database that stores data in a flexible, document-based format. You can download MongoDB from the official website (https://www.mongodb.com/try/download/community).
Once you have installed MongoDB, you can start the MongoDB server by running the following command in your terminal:
mongod
Next, we need to install PySimpleGUI, a Python GUI library that allows us to easily create user interfaces for our applications. You can install PySimpleGUI using pip:
pip install PySimpleGUI
Now that we have all the necessary tools installed, we can start building our application. We will create a simple GUI application that allows users to interact with our MongoDB database by adding, updating, and deleting records.
First, let’s create a Python script to connect to our MongoDB database using the PyMongo library. PyMongo is a Python driver for MongoDB that allows us to interact with the database from our Python code.
import pymongo
# Connect to the MongoDB database
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["mydatabase"]
collection = db["customers"]
Next, let’s create a PySimpleGUI window that allows users to add, update, and delete records in our MongoDB database.
import PySimpleGUI as sg
layout = [
[sg.Text('Name:'), sg.InputText(key='name')],
[sg.Text('Email:'), sg.InputText(key='email')],
[sg.Button('Add'), sg.Button('Update'), sg.Button('Delete')],
[sg.Multiline(size=(50, 10), key='output')]
]
window = sg.Window('MongoDB GUI', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'Add':
name = values['name']
email = values['email']
record = {'name': name, 'email': email}
collection.insert_one(record)
window['output'].update(f'Added record: {record}n', append=True)
if event == 'Update':
name = values['name']
email = values['email']
updated_record = {'name': name, 'email': email}
collection.update_one({'name': name}, {'$set': updated_record})
window['output'].update(f'Updated record: {updated_record}n', append=True)
if event == 'Delete':
name = values['name']
collection.delete_one({'name': name})
window['output'].update(f'Deleted record: {name}n', append=True)
window.close()
To run the application, save the script to a file (e.g., mongodb_gui.py
) and run it using Python:
python mongodb_gui.py
You should see a window with input fields for name and email, as well as buttons to add, update, and delete records in the MongoDB database. You can enter a name and email, click the "Add" button to add a record, update the name or email and click the "Update" button to update a record, and enter a name and click the "Delete" button to delete a record.
In this tutorial, we have learned how to connect to a MongoDB database using PyMongo, create a simple GUI application using PySimpleGUI, and interact with the database by adding, updating, and deleting records. This is just a starting point, and you can further customize the application to suit your needs. Happy coding!