Why not change things up instead of solely using the online database system to store data? Being unusual is always exciting for the modern day programmer.
Often wondering if instead of using an online database, I could save data on an individual’s device. That way, I felt it to be more secure. Putting myself to the test I worked on my project. It was quite the challenge. I used the Javascript localstorage API to achieve my purpose.
Strings can be stored as persistent key-value pairs in the localStorage object. All other windows and frames from the same origin quickly display any changes. Unless the user deletes saved data or configures an expiration limit, the stored values are permanent indefinitely. For accessing and setting values, localStorage employs a map-like interface.
localStorage.setItem(“key”, “value”);
localStorage.getItem(“key”);
localStorage.removeItem(key);
localStorage.clear()
Here is a link to the code github code
Another difficulty arose since the localStorage could only hold roughly 5 MB of data.Because it couldn't be used for important purposes, this presented a significant challenge that had to be overcomed.As you could have predicted I found another way. The way? The python shelf library.
It was defined as a “persistent, dictionary-like object. The difference with “dbm” databases is that the values (not the keys!) in a shelf can be essentially arbitrary Python objects — anything that the pickle module can handle”.
You can read more at https://docs.python.org/3/library/shelve.html.
How did I come up with this idea? I was reading ‘Al Sweigart’s Automate the boring stuff with Python’ which is one of my favorite programming books. It’s a fantastic book that I highly recommend.
It is a simple yet effective tool for permanent data storage, as well as a dictionary-like object that is persistently saved in a disk file, in simpler terms.
import shelve
shelfFile = shelve.open(‘mydata’)
cats = [‘Zophie’, ‘Pooka’, ‘Simon’]
shelfFile[‘cats’] = cats
shelfFile.close()
With that as my base, I was able to construct thatnoteapp
I succeeded in creating the easiest thing I could have hoped to. Here is a brief explanation of how I managed to do that
In this situation, storing data in an list would be ideal because they are incredibly versatile and simple to manage.
noteObject = shelve.open('daisdb')
note = []
noteObject['notes'] = note
noteObject.close()
To create new notes, i used a dictionary to store its keys and values. After appending it to the list, we now have a list of of dictionaries
def new():
obj = {
‘ids’:ids,
‘title’:title,
‘content’: data,
‘date’: current_date
}
noteObject = shelve.open(‘daisdb’, writeback = True)
noteObject[‘notes’].append(obj)
noteObject.sync()
noteObject.close()
return redirect(‘/’)
And this was how I sent it to the front end.
def index():
noteObject = shelve.open(‘daisdb’, ‘r’, writeback = True)
notes = noteObject[‘notes’]
return render_template(‘index.html’,notes=notes)
The numerous projects that could result from this post interest me; Why not be one of the programmers who would try it out?
Edit: I wrote a full tutorial on this project.
Top comments (0)