# if a database button has NOT been clicked yetĭb_label. # change the text for the label if DB is selected
Print ( " \nbutton_list:", button_list ) # print the final list after the loop is complete # add the widget to the layout at the end of iteration # use Python's lamda feat to pass a func to button's on_press() Print ( " \nMongoDB db:", num, '-', index )
# enumerate the list of database names to create Kivy widgetsįor num, index in enumerate (database_names ): # eval connection to MongoDB with global client instance # instantiate button without eval() by putting them in a list layout = BoxLayout (orientation = 'vertical' ) # set the layout for the Kivy application mongo_domain = str (DOMAIN ) + str (PORT ) # concatenate the host's domain and port variables selected_db = None # None selected by default # declare a new object for the selected Mongo collection # define the build() function for the app # create a new class for the Kivy MongoDB app # global variables for MongoDB host (default port is 27017)
Declare a Kivy App Python class for the MongoDB GUI applicationĭeclare a new class with Python’s class keyword for the Kivy application by passing the entire App library to the class declaration as an argument so that it can inherit all of Kivy’s classes and methods:įrom kivy.
The example code in this article uses a 3 seconds ( serverSelectionTimeoutMS = 3000) timeout to attempt to connect before raising an exception and displaying the connection error in the Kivy app. The list of database names will be used to populate a list of Kivy Button() widgets later on. Unlike the first article, however, the code above creates a global list object containing all of the MongoDB server’s databases, but will return an empty list if the connection failed, or if the API call, for whatever reason, returns a ServerSelectionTimeoutError exception. # set the client and DB name list to 'None' and `` if exception # get the database_names from the MongoClient()ĭatabase_names = MongoClient ( ). # print the version of MongoDB server if connection successful ServerSelectionTimeoutMS = 3000 # 3 second timeout # use a try-except indentation to catch MongoClient() errors
Here are the PIP3 commands once again to install the Kivy and MongoDB libraries for Python:
The first part of the series demonstrated how to install the Python packages for the MongoDB app. Install the PyMongo distribution using the pip3 command if you haven’t done so already: Use the mongod or mongodb command, in a terminal or command prompt window, to ensure that the server is running. The MongoDB server should be running on your localhost on the same machine that will execute the Python script for the Kivy application. The script has not been tested on Python 2, and Python 2.7 is losing support and is now deprecated. It’s recommended that you use Python 3 for the example code in this article.
Prerequisites for creating a MongoDB GUI application with Python and Kivy This article will show how to create button and label widgets for the app, as well how to make API calls to MongoDB, using PyMongo, and then use the returned data to change the Kivy label widgets. The last part showed how to install the libraries, connect to the MongoDB server, and finally setup the Kivy application class. The completed application in this article will allow the user to access the databases on a MongoDB localhost server, and retrieve their respective collection names, by making PyMongo API calls inside of the Kivy App class. This article demonstrates how to build a GUI application for MongoDB, that can be deployed to multiple platforms, using the Kivy framework and Python programming language in less than 200 lines of code.