Setting up Hot Reload with Kivy & KivyMD using Kaki for an Easy Development Workflow – Part 2 by Novfensec Inc.

Posted by

In this tutorial, we will walk you through setting up Kivy & KivyMD Hot Reload with Kaki for an easy development workflow. This setup will allow you to quickly test your code changes and see them reflected in real-time without having to restart the application.

Before we begin, make sure you have Kivy and KivyMD installed on your system. If you haven’t installed them yet, you can do so by following the instructions on their respective websites.

First, we need to install Kaki, which is a tool that enables hot reload functionality for Kivy applications. To do this, open a terminal window and run the following command:

pip install kaki

Next, create a new Python file for your Kivy app. In this tutorial, we will create a simple app that displays a text label.

<!DOCTYPE html>
<html>
<head>
    <title>My Kivy App</title>
</head>
<body>
</body>
</html>

Save the file as "my_app.py" in your project directory.

Next, we need to create a new file called "kaki_config.py" in the same directory. This file will contain the configuration settings for Kaki.

<!DOCTYPE html>
<html>
<head>
    <title>Kaki Configuration</title>
</head>
<body>
</body>
</html>

Add the following code to the "kaki_config.py" file:

from kaki.app import App as KakiApp
from kivy.app import App as KivyApp

KV_DIRECTORY = 'kv_files'
KV_FILE = 'my_app.kv'

In this code snippet, we are specifying the directory where our Kivy language files will be located and the name of the main Kivy language file for our app.

Next, create a new directory called "kv_files" in your project directory. This is where you will store your Kivy language files.

Now, let’s create the Kivy language file for our app. Create a new file called "my_app.kv" in the "kv_files" directory.

<!DOCTYPE html>
<html>
<head>
    <title>Kivy Language File</title>
</head>
<body>
</body>
</html>

Add the following code to the "my_app.kv" file:

<MyLabel>:
    text: 'Hello, World!'

This code defines a new Kivy widget called "MyLabel" with a text property set to ‘Hello, World!’.

Now, let’s create the main Python file for our Kivy app. Open the "my_app.py" file and add the following code:

from kivy.app import App
from kivy.uix.label import Label

class MyLabel(Label):
    pass

class MyApp(App):
    def build(self):
        return MyLabel()

if __name__ == '__main__':
    MyApp().run()

This code creates a simple Kivy app with a single label widget.

Now that we have all the necessary files in place, we can run our app using Kaki’s hot reload functionality. Open a terminal window and navigate to your project directory.

Run the following command to start the Kaki server:

python -m kaki

Next, run the following command to start the Kivy app:

python my_app.py

You should see the Kivy app window open with the text ‘Hello, World!’ displayed.

Now, make a change to the text property of the label in the "my_app.kv" file. For example, change the text to ‘Welcome to Kivy!’.

Save the file, and you should see the change reflected in the Kivy app window without having to restart the application.

That’s it! You have successfully set up Kivy & KivyMD hot reload with Kaki for an easy development workflow. This setup will save you time and make it easier to test and debug your Kivy applications.

Stay tuned for more tutorials from Novfensec Inc. Happy coding!