- Kivy Tutorial
- Kivy - Home
- Kivy Basics
- Kivy - Getting Started
- Kivy - Installation
- Kivy - Architecture
- Kivy - File Syntax
- Kivy - Applications
- Kivy - Hello World
- Kivy - App Life Cycle
- Kivy - Events
- Kivy - Properties
- Kivy - Inputs
- Kivy - Behaviors
- Kivy Buttons
- Kivy - Buttons
- Kivy - Button Events
- Kivy - Button Colors
- Kivy - Button Size
- Kivy - Button Position
- Kivy - Round Buttons
- Kivy - Disabled Buttons
- Kivy - Image Button
- Kivy Widgets
- Kivy - Widgets
- Kivy - Label
- Kivy - Text Input
- Kivy - Canvas
- Kivy - Line
- Kivy - Checkbox
- Kivy - Dropdown List
- Kivy - Windows
- Kivy - ScrollView
- Kivy - Carousel
- Kivy - Slider
- Kivy - Images
- Kivy - Popup
- Kivy - Switch
- Kivy - Spinner
- Kivy - Splitter
- Kivy - Progress Bar
- Kivy - Bubble
- Kivy - Tabbed Panel
- Kivy - Scatter
- Kivy - Accordion
- Kivy - File Chooser
- Kivy - Color Picker
- Kivy - Code Input
- Kivy - Modal View
- Kivy - Toggle Button
- Kivy - Camera
- Kivy - Tree View
- Kivy - reStructuredText
- Kivy - Action Bar
- Kivy - Video Player
- Kivy - Stencil View
- Kivy - VKeyboard
- Kivy - Touch Ripple
- Kivy - Audio
- Kivy - Videos
- Kivy - Spelling
- Kivy - Effects
- Kivy - Input Recorder
- Kivy - OpenGL
- Kivy - Text
- Kivy - Text Markup
- Kivy - Settings
- Kivy Layouts
- Kivy - Layouts
- Kivy - Float Layout
- Kivy - Grid Layouts
- Kivy - Box Layouts
- Kivy - Stack Layout
- Kivy - Anchor Layout
- Kivy - Relative Layout
- Kivy - Page Layout
- Kivy - Recycle Layout
- Kivy - Layouts in Layouts
- Kivy Advanced Concepts
- Kivy - Configuration Object
- Kivy - Atlas
- Kivy - Data Loader
- Kivy - Cache Manager
- Kivy - Console
- Kivy - Animation
- Kivy - Multistroke
- Kivy - Clock
- Kivy - SVGs
- Kivy - UrlRequest
- Kivy - Clipboard
- Kivy - Factory
- Kivy - Gesture
- Kivy - Language
- Kivy - Graphics
- Kivy - Drawing
- Kivy - Packaging
- Kivy - Garden
- Kivy - Storage
- Kivy - Vector
- Kivy - Utils
- Kivy - Inspector
- Kivy - Tools
- Kivy - Logger
- Kivy - Framebuffer
- Kivy Applications and Projects
- Kivy - Drawing App
- Kivy - Calculator App
- Kivy - Stopwatch App
- Kivy - Camera Handling
- Kivy - Image Viewer
- Kivy - Bezier
- Kivy - Canvas Stress
- Kivy - Circle Drawing
- Kivy - Widget Animation
- Kivy - Miscellaneous
- Kivy Useful Resources
- Kivy - Quick Guide
- Kivy - Useful Resources
- Kivy - Discussion
Kivy - Storage
The Storage class in Kivy framework is provided to load and store any number of key-value pairs via an indexed entry. The "kivy.storage" module defines the AbstractStore class. Its implementations − DictStore, JsonStore and RedisStore − provide the concrete classes.
kivy.storage.dictstore.DictStore: use a python dict as a store.
kivy.storage.jsonstore.JsonStore: use a JSON file as a store.
kivy.storage.redisstore.RedisStore: use a Redis database with redis-py.
To use any of the above storage classes, import the relevant class, declare an object and call its put() method to store the k-v pairs. For JsonStore −
from kivy.storage.jsonstore import JsonStore store = JsonStore('hello.json') # put some values store.put(name, key1=val1, key2=val2)
This will create hello.json file in the current directory. You can retrieve the information with get() method.
print (store.get(name)[key])
Following methods are defined in AbstractStore class, which need to be overridden by the concrete implementations like DictStore −
clear() − Wipe the whole storage.
count() − Return the number of entries in the storage.
delete(key) − Delete a key from the storage. If the key is not found, a KeyError exception will be thrown.
exists(key) − Check if a key exists in the store.
find(**filters) − Return all the entries matching the filters. The entries are returned through a generator as a list of (key, entry) pairs where entry is a dict of key-value pairs.
get(key) − Get the key-value pairs stored at key. If the key is not found, a KeyError exception will be thrown.
keys() − Return a list of all the keys in the storage.
put(key, **values) − Put new key-value pairs (given in values) into the storage. Any existing key-value pairs will be removed.
The methods (get(), put() , exists(), delete(), find()) have an asynchronous version. These methods can be called with or without a callback parameter. If given, the callback returns the result to the user when available, as the request will be asynchronous. If the callback is None, then the request will be synchronous and the result will be returned directly.
Example
Here is the example −
# synchronous res=store.get(key) print (res) # asynchronous def my_callback(store, key, result): print (result) store.get(key)
The callback function should have these following parameters −
store − the 'Store' instance currently used.
key − the key sought for.
result − the result of the lookup for the key.
Example
from kivy.storage.jsonstore import JsonStore from kivy.storage.dictstore import DictStore store = JsonStore('store.json') # put some values store.put('state', name='Maharashtra', capital='Mumbai', population='Eleven Cr') store.put('os', name='Windows', version=11, released=2021) store.put('shape', type='circle', radius=5) # using the same index key erases all previously added k-v pairs # get a value using a index key and key print('Population of ', store.get('state')['name'], 'is ', store.get('state')['population']) print (store.get('state').keys()) for k,v in store.get('state').items(): print (k,":",v) # or guess the key/entry for a part of the key for item in store.find(type='circle'): print('Store:',item[0]) print('K-V pairs: ',str(item[1]))
Output
It will produce the following output −
Population of Maharashtra is Eleven Cr dict_keys(['name', 'capital', 'population']) name : Maharashtra capital : Mumbai population : Eleven Cr Store: shape K-V pairs: {'type': 'circle', 'radius': 5}
To Continue Learning Please Login
Login with Google