Hey, you're still there.
I know this is a bit of a long project, but stick with me.
We're getting really, really close.
You see, up until now, we've built this whole system, right?
We have our HTML, CSS and JavaScript files that we got free templates for, and we also implemented
our server so that the browser can actually send data to us and we can actually save that data and even
send a thank you HTML page back once somebody submits a form.
Our web page is now something more dynamic than just text that people can read.
People can now interact with our web page by submitting a form, and we now have sent this form data
to our Python server.
But there was a problem that I mentioned in the last video.
That is, as soon as the server stops running or maybe it crashes, that data is lost because right
now that information of the form is in memory.
Ideally, we write it somewhere on disk so that even if we stop the server, that information is still
somewhere on a file.
So that this file lives on this computer, just like a file lives on your desktop or laptop.
So how can we do this?
Well, we've done this before.
Right.
We simply write to a file.
Now we're going to explore this topic a little bit more throughout the next couple of videos.
But what I want to do is to close off some of these folders and create a new file in here.
That will be a text file and this file will just be called database dot txt.
Just a simple text file.
And this database will act as our database where we store information that the server receives.
So if you remember, we're going to store email, we're going to store a subject, and then we're going
to store the message.
So ideally we're able to append or add to this file as people submit the form.
So how can we do this?
If you want to pause the video and give it a try yourself.
Otherwise, let's try this.
We can create a new function here.
Right.
To.
And this right to file will receive some sort of data that will send.
And we're going to say with.
Open again, something that we've seen before, and we're going to open the database and again, because
it's in the same folder as the server.
We don't need to specify a file path.
We can say database dot text.
And we're going to open in the mode of a so that is to append to that file because it already exists
and we'll just call it as database.
And in here, we'll say that the file will equal.
Database.
Dot.
Right.
And then.
Right what we want.
In our case.
We want to write the email.
The message or the subject and then the message.
And we can just extract that from our data, right, that we're going to pass in so we can say that
email is going to equal the data.
Email.
Subject is going to equal data subject.
And their message is going to equal data.
Message because it's a dictionary, right?
And then all we can do here is just do an F string and say that we want we want, first of all, a new
line so that there's a new line every time there's an entry.
And we'll say that we want the email, comma, the subject, and then finally comma the message.
So that's our right to file function.
And then in here, instead of printing data, we can just simply call, write to file.
And pass in the data.
So let's save this and see what happens.
I'm going to come back to the page and then click Send.
All right.
Looks like things have worked, but let's see if we actually wrote to file.
If I go to database.
Look at that.
I have my data.
That is the email, the subject and the message.
And if I go back to the page, let's say go to contact again.
And this time let's do a different email.
Some texts, some more text hit send and let's go back and how cool is that?
We now have our own custom database where we store all these users information.
And the cool part is that even if I close the server.
I still have this information with me.
I can always come back and see who has submitted a form.
Very, very cool.
And if by now you realize this is how databases work.
Well, good for you.
This is how databases work.
This is why you hear the words database.
A database is essentially a place for you to store data in a safe way outside of the server.
Right.
Because if your information is in memory, as soon as the computer closes.
Well, that memory is gone.
Instead, a text file like this, even if I shut down my computer and turn it back on, is always going
to be on disk.
And it's what we call persisting.
That is, it persists so that it never gets deleted.
But how can we make this better?
Well, right now this is just a text file.
This is kind of inconvenient and hard to read.
Maybe we're creating a startup landing page and we're collecting users information, maybe email so
we can email people when these startup launches.
A better way to create a database is maybe using a CSV file or an Excel file.
So in the next video, let's explore that.