Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+466 -333
View File
@@ -1,347 +1,480 @@
---
description: This rule file outlines best practices for Kivy UI development, including code organization, performance, security, and testing. Adhering to these guidelines ensures maintainable, efficient, and secure Kivy applications.
globs: *.py,*.kv
description: Definitive guide for building modern, maintainable Kivy applications using best practices, focusing on KV language, KivyMD, and clear code organization.
globs: **/*.py
---
# Kivy Development Best Practices
# Kivy Best Practices
This document provides a comprehensive guide to Kivy development best practices, covering various aspects from code organization to security and testing.
This guide outlines the definitive best practices for developing Kivy applications. Adhere to these rules to ensure your codebase is maintainable, performant, and scales effectively.
## 1. Code Organization and Structure
## 1. Prioritize KV Language for UI Definition
Proper code organization is crucial for maintainability, scalability, and collaboration in Kivy projects.
**Opinion:** Always separate your UI definition from Python logic using Kivy Language (`.kv` files). This is the cornerstone of maintainable Kivy applications and enables declarative UI. Avoid building complex widget trees imperatively in Python.
### 1.1 Directory Structure
**When to use:** For all visual components, layouts, bindings, and animations.
A well-defined directory structure helps organize your Kivy project. Here's a suggested structure:
**❌ BAD (Imperative Python UI):**
```python
# main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class BadApp(App):
def build(self):
layout = BoxLayout(orientation='vertical')
button1 = Button(text='Click Me 1')
button2 = Button(text='Click Me 2')
layout.add_widget(button1)
layout.add_widget(button2)
return layout
my_kivy_app/
├── app.py # Main application file
├── main.kv # Root KV file
├── screens/ # Directory for screen definitions
│ ├── __init__.py # Initialize screens package
│ ├── main_screen.py
│ ├── main_screen.kv
BadApp().run()
```
**✅ GOOD (Declarative KV Language):**
```python
# main.py
from kivy.app import App
from kivy.lang import Builder
class GoodApp(App):
def build(self):
# Kivy automatically loads 'goodapp.kv' if not specified
return Builder.load_file('goodapp.kv')
GoodApp().run()
```
```kv
# goodapp.kv
BoxLayout:
orientation: 'vertical'
Button:
text: 'Click Me 1'
Button:
text: 'Click Me 2'
```
## 2. Leverage KivyMD for Material Design
**Opinion:** For modern, polished UIs, use KivyMD. It provides a rich set of Material Design components that integrate seamlessly with Kivy's KV language.
**When to use:** For all new UI development requiring a modern aesthetic.
**❌ BAD (Vanilla Kivy for complex components):**
```kv
# my_screen.kv
BoxLayout:
# ... manually style a button to look like Material Design ...
Button:
background_color: 0.1, 0.5, 0.8, 1
color: 1, 1, 1, 1
padding: dp(16), dp(8)
# ... many more properties ...
```
**✅ GOOD (KivyMD components):**
```kv
# my_screen.kv
MDBoxLayout: # Use MD layouts for consistency
orientation: 'vertical'
MDTopAppBar:
title: "KivyMD App"
MDButton:
MDButtonText:
text: "Material Button"
on_release: app.root.ids.label.text = "Button pressed!"
MDLabel:
id: label
text: "Hello KivyMD"
halign: 'center'
```
```python
# main.py
from kivymd.app import MDApp
from kivy.lang import Builder
class MDExampleApp(MDApp):
def build(self):
# Load KV string directly for simple examples, or use Builder.load_file('my_screen.kv')
return Builder.load_string("""
MDBoxLayout:
orientation: 'vertical'
MDTopAppBar:
title: "KivyMD App"
MDLabel:
text: "Welcome to KivyMD!"
halign: 'center'
""")
MDExampleApp().run()
```
## 3. Structure Your Project Modularly
**Opinion:** Organize your Kivy project into logical directories for maintainability and scalability. Separate KV files, Python logic, screens, and custom widgets.
**When to use:** For any project beyond a simple single-file example.
**❌ BAD (Monolithic structure):**
```
my_app/
├── main.py
├── all_screens_and_widgets.kv
├── images/
```
**✅ GOOD (Modular structure):**
```
my_app/
├── main.py
├── screens/
│ ├── home_screen.py
│ └── home_screen.kv
│ ├── settings_screen.py
│ └── settings_screen.kv
├── components/ # Directory for reusable UI components
│ ├── __init__.py # Initialize components package
├── widgets/
│ ├── custom_button.py
│ └── custom_button.kv
├── utils/ # Utility modules
│ ├── __init__.py
│ └── helpers.py
├── data/ # Data files (JSON, images, fonts, etc.)
├── kv/ # For global styles or shared rules
│ └── styles.kv
├── assets/
│ ├── images/
│ ├── fonts/
│ └── settings.json
├── tests/ # Unit and integration tests
│ ├── __init__.py
│ ├── test_main_screen.py
│ └── test_helpers.py
├── .gitignore # Git ignore file
├── README.md # Project README
└── requirements.txt # Project dependencies
* `app.py`: The main application file where you define your `App` subclass and run the application.
* `main.kv`: The root KV file that typically handles overall layout structure and screen management. It is loaded automatically if named appropriately (e.g., `myapp.kv` for a `MyApp` class).
* `screens/`: Contains individual screen definitions. Each screen can have a Python file for logic and a KV file for layout.
* `components/`: Holds reusable UI components, like custom buttons, labels, or layouts. This promotes code reuse and consistency.
* `utils/`: Includes helper functions and utility modules that provide common functionalities across the application.
* `data/`: Stores application data like images, fonts, and configuration files.
* `tests/`: Contains unit and integration tests to ensure code quality and prevent regressions.
* `.gitignore`: Specifies intentionally untracked files that Git should ignore.
* `README.md`: Provides a project overview, setup instructions, and other relevant information.
* `requirements.txt`: Lists the Python packages required to run the application. Use `pip freeze > requirements.txt` to generate this.
### 1.2 File Naming Conventions
* **Python files:** Use lowercase with underscores (snake_case), e.g., `main_screen.py`, `custom_button.py`.
* **KV files:** Use the same name as the corresponding Python file, but with the `.kv` extension, e.g., `main_screen.kv`, `custom_button.kv`.
* **Class names:** Use CamelCase, e.g., `MainScreen`, `CustomButton`.
* **Variables and functions:** Use snake_case, e.g., `screen_width`, `calculate_layout()`.
Consistent naming conventions improve code readability and maintainability.
### 1.3 Module Organization
* **Separate concerns:** Divide your application into logical modules based on functionality. For example, a module for data access, a module for UI components, and a module for business logic.
* **Use packages:** Group related modules into packages using `__init__.py` files. This helps organize your project and prevent naming conflicts.
* **Keep modules small:** Avoid creating overly large modules. Smaller modules are easier to understand, test, and reuse.
* **Use relative imports:** Within a package, use relative imports (e.g., `from . import helpers`) to refer to other modules in the same package. This makes your code more portable and less prone to errors.
### 1.4 Component Architecture
* **Create reusable components:** Design your UI with reusable components. This promotes code reuse, reduces redundancy, and makes it easier to maintain your application. Kivy's KV language and custom widget creation are ideal for this.
* **Use custom widgets:** Create custom widgets by subclassing Kivy's existing widgets or combining multiple widgets. This allows you to encapsulate complex UI elements and behavior into reusable components.
* **Follow the DRY principle (Don't Repeat Yourself):** Abstract common functionalities into reusable components or utility functions. This reduces code duplication and makes your code more maintainable.
### 1.5 Code Splitting
* **Break down large screens:** If a screen becomes too complex, break it down into smaller, manageable sub-components.
* **Lazy loading:** Load parts of the UI or data only when they are needed. This can improve startup time and reduce memory usage.
* **Dynamic loading of KV files:** Load KV files dynamically using `Builder.load_file()` when a screen or component is needed. This allows you to split your UI definition into multiple files and load them on demand.
## 2. Common Patterns and Anti-patterns
Understanding common design patterns and anti-patterns helps you write better Kivy code.
### 2.1 Design Patterns
* **Model-View-Controller (MVC) / Model-View-Presenter (MVP) / Model-View-ViewModel (MVVM):** Consider using these patterns to separate data (model), UI (view), and logic (controller/presenter/viewmodel). Kivy does not enforce a specific architecture, but these patterns can help organize your code and improve testability. MVVM is often considered more suitable due to Kivy's declarative nature.
* **Observer:** Kivy's properties and event dispatching mechanism are based on the Observer pattern. Use properties to observe changes in data and trigger UI updates accordingly.
* **Factory:** Use the Factory pattern to create different types of widgets or objects based on certain conditions or data. This can be useful for dynamically generating UI elements.
* **Singleton:** While often debated, Singletons can be useful for global configuration or access to shared resources. Use with caution and consider alternatives like dependency injection.
### 2.2 Recommended Approaches
* **Use KV language for UI definition:** Separate your UI definition from your Python code using the KV language. This improves code readability and maintainability.
* **Bind properties to UI elements:** Use Kivy's property binding mechanism to automatically update UI elements when data changes. This simplifies UI updates and reduces boilerplate code.
* **Use events for user interaction:** Use Kivy's event system to handle user interactions, such as button clicks and text input. This allows you to react to user actions and update the UI accordingly.
* **Asynchronous operations:** Use asynchronous operations (e.g., `Clock.schedule_once()`, `Clock.schedule_interval()`, or `async/await`) for long-running tasks to avoid blocking the UI thread. This ensures that your application remains responsive.
* **Data binding with properties:** Leverage Kivy properties (StringProperty, NumericProperty, ObjectProperty, etc.) for data binding between UI elements and your application's data model. This enables automatic updates of UI elements when the underlying data changes, and vice versa.
### 2.3 Anti-patterns and Code Smells
* **Tight coupling:** Avoid tight coupling between UI elements and business logic. Separate your UI definition from your logic using the KV language and appropriate design patterns.
* **God object:** Avoid creating overly large and complex classes that handle too many responsibilities. Break down large classes into smaller, more manageable classes.
* **Code duplication:** Avoid duplicating code. Abstract common functionalities into reusable components or utility functions.
* **Ignoring errors:** Don't ignore errors. Handle errors gracefully and provide meaningful feedback to the user.
* **Blocking the UI thread:** Avoid performing long-running tasks on the UI thread. Use asynchronous operations to prevent the UI from freezing.
* **Excessive use of `eval()` or `exec()`:** Avoid using `eval()` or `exec()` to execute arbitrary code, as this can introduce security vulnerabilities. Prefer safer alternatives like data binding or function calls.
* **Over-reliance on global variables:** Minimize the use of global variables, as they can make your code harder to understand and maintain. Prefer passing data explicitly between components.
### 2.4 State Management
* **Kivy Properties:** For simple state management within a widget or screen, Kivy Properties are often sufficient. They offer automatic notification when their value changes, making them easy to bind to UI elements.
* **ScreenManager:** The `ScreenManager` widget can store state associated with each screen. This is useful for managing the overall application state and passing data between screens.
* **External state management libraries:** For more complex state management requirements, consider using external libraries like: `RxPY`, `KivyMD`, or even integrating with more comprehensive frameworks like Flask for the backend.
* **Minimize mutable state:** Prefer immutable data structures to reduce the risk of unexpected side effects and improve code predictability.
* **Centralized State:** Implement a centralized state management system to ensure data consistency across different parts of the application.
### 2.5 Error Handling
* **Use `try...except` blocks:** Use `try...except` blocks to handle potential exceptions. Provide specific exception handling for different types of errors.
* **Log errors:** Log errors to a file or console for debugging purposes. Include relevant information, such as the error message, stack trace, and timestamp.
* **Provide user-friendly error messages:** Display user-friendly error messages to the user. Avoid displaying technical details that the user may not understand.
* **Handle unhandled exceptions:** Implement a global exception handler to catch unhandled exceptions and prevent the application from crashing. Log the error and display a generic error message to the user.
* **Retry mechanism:** Implement a retry mechanism for operations that may fail due to transient errors, such as network connectivity issues.
## 3. Performance Considerations
Optimizing performance is crucial for creating smooth and responsive Kivy applications.
### 3.1 Optimization Techniques
* **Use the right layout:** Choose the most efficient layout for your UI. `RelativeLayout` and `FloatLayout` can be more expensive than `BoxLayout` or `GridLayout`.
* **Minimize widget count:** Reduce the number of widgets in your UI. Consider using custom drawing or more efficient layouts to achieve the same visual effect with fewer widgets.
* **Use textures efficiently:** Load images and textures at the correct size. Avoid scaling large images down in the UI, as this can be inefficient. Use texture atlases to combine multiple small images into a single texture, reducing the number of texture swaps.
* **Use `AsyncImage` for loading images:** Load images asynchronously using `AsyncImage` to avoid blocking the UI thread.
* **Optimize drawing:** Use Kivy's drawing instructions efficiently. Batch drawing operations to reduce the number of OpenGL calls.
* **Freeze layouts:** Use `Widget.size_hint` with fixed values to help Kivy optimize layout calculations.
* **Use appropriate property types:** Use specific Kivy property types (StringProperty, NumericProperty, etc.) instead of generic ObjectProperty to improve performance.
* **Avoid complex calculations in property callbacks:** Keep property callback functions short and simple. Move complex calculations to separate functions and call them from the callbacks.
### 3.2 Memory Management
* **Release unused resources:** Release unused resources, such as images and textures, when they are no longer needed. Use `Widget.clear()` to remove all children from a widget and release their resources.
* **Use weak references:** Use weak references to avoid creating circular dependencies that can prevent objects from being garbage collected.
* **Avoid creating large lists or dictionaries:** If you need to store large amounts of data, consider using more efficient data structures, such as NumPy arrays or SQLite databases.
* **Use generators:** Use generators to process large data sets lazily, reducing memory consumption.
* **Profile your application:** Use a memory profiler to identify memory leaks and optimize memory usage.
### 3.3 Rendering Optimization
* **Use the correct OpenGL context:** Ensure that you are using the correct OpenGL context for your platform. Kivy supports both OpenGL and OpenGL ES.
* **Reduce overdraw:** Reduce overdraw by minimizing the number of overlapping UI elements.
* **Use shaders:** Use shaders to perform complex rendering effects efficiently.
* **Optimize texture loading:** Optimize texture loading by using compressed textures and mipmaps.
* **Use the `kivy.graphics` module efficiently:** Minimize the number of drawing calls and state changes. Batch drawing operations together.
### 3.4 Bundle Size Optimization
* **Remove unused code:** Remove unused code from your application to reduce the bundle size.
* **Compress images:** Compress images to reduce their file size. Use lossless compression for images that require high quality and lossy compression for images where some quality loss is acceptable.
* **Use efficient audio formats:** Use efficient audio formats, such as MP3 or Opus, to reduce the size of your audio files.
* **Obfuscate your code:** Obfuscate your code to make it harder to reverse engineer and reduce the bundle size.
* **Use code minification:** Minify your code to remove unnecessary whitespace and comments, reducing the bundle size.
* **Use code splitting:** Split your code into multiple modules and load them on demand to reduce the initial bundle size.
### 3.5 Lazy Loading
* **Load screens on demand:** Load screens only when they are needed. This can improve startup time and reduce memory usage. Use `ScreenManager.add_widget()` and `ScreenManager.remove_widget()` to manage screens dynamically.
* **Load data on demand:** Load data only when it is needed. Use asynchronous operations to load data in the background without blocking the UI thread.
* **Load images on demand:** Load images only when they are needed. Use `AsyncImage` to load images asynchronously.
* **Virtualization for lists:** When displaying long lists, use virtualization techniques to only render the visible items. This can significantly improve performance and reduce memory usage.
## 4. Security Best Practices
Security should be a primary concern when developing Kivy applications, especially those that handle sensitive data or interact with external services.
### 4.1 Common Vulnerabilities
* **Code injection:** Avoid using `eval()` or `exec()` to execute arbitrary code, as this can allow attackers to inject malicious code into your application.
* **Cross-site scripting (XSS):** Be careful when displaying user-generated content in your UI. Sanitize the content to prevent attackers from injecting malicious scripts.
* **SQL injection:** When interacting with databases, use parameterized queries or object-relational mappers (ORMs) to prevent SQL injection attacks.
* **Man-in-the-middle (MITM) attacks:** Use HTTPS to encrypt communication between your application and external services, preventing attackers from intercepting sensitive data.
* **Data leakage:** Be careful not to leak sensitive data in your application's logs or error messages. Disable debug mode in production builds.
* **Insecure storage:** Avoid storing sensitive data in plain text. Encrypt sensitive data before storing it locally or in the cloud.
### 4.2 Input Validation
* **Validate all user input:** Validate all user input to ensure that it conforms to the expected format and range. Use regular expressions or custom validation functions to validate input.
* **Sanitize user input:** Sanitize user input to remove potentially harmful characters or code. Use Kivy's built-in functions or external libraries to sanitize input.
* **Limit input length:** Limit the length of input fields to prevent buffer overflows and other security vulnerabilities.
* **Escape output:** Escape output to prevent cross-site scripting (XSS) attacks. Use Kivy's built-in functions or external libraries to escape output.
* **Use whitelisting:** Use whitelisting to allow only specific characters or patterns in input fields. This can be more secure than blacklisting, which attempts to block specific harmful characters or patterns.
### 4.3 Authentication and Authorization
* **Use secure authentication protocols:** Use secure authentication protocols, such as OAuth 2.0 or JWT, to authenticate users. Avoid storing passwords in plain text.
* **Implement multi-factor authentication (MFA):** Implement multi-factor authentication (MFA) to add an extra layer of security to user accounts.
* **Use role-based access control (RBAC):** Use role-based access control (RBAC) to restrict access to sensitive data and functionalities based on user roles.
* **Regularly audit your authentication and authorization system:** Regularly audit your authentication and authorization system to identify and fix any vulnerabilities.
* **Use password hashing:** Always hash passwords using a strong hashing algorithm (e.g., bcrypt or Argon2) before storing them. Never store passwords in plain text.
### 4.4 Data Protection
* **Encrypt sensitive data:** Encrypt sensitive data before storing it locally or in the cloud. Use strong encryption algorithms, such as AES.
* **Use secure storage:** Store sensitive data in secure storage locations, such as the Android Keystore or iOS Keychain.
* **Protect data in transit:** Use HTTPS to encrypt communication between your application and external services.
* **Comply with data privacy regulations:** Comply with data privacy regulations, such as GDPR and CCPA.
* **Implement data masking:** Mask sensitive data when displaying it in the UI or logs. This can prevent unauthorized access to sensitive information.
### 4.5 Secure API Communication
* **Use HTTPS:** Always use HTTPS to communicate with APIs. This encrypts the data transmitted between your application and the API server, preventing eavesdropping.
* **Validate API responses:** Validate API responses to ensure that they are in the expected format and do not contain any malicious data.
* **Use API keys or tokens:** Use API keys or tokens to authenticate your application with the API server. Store API keys and tokens securely.
* **Rate limiting:** Implement rate limiting to prevent denial-of-service attacks. This limits the number of requests that a client can make to the API server within a given time period.
* **Input validation on the server-side:** Validate all input data on the server-side, even if you have already validated it on the client-side. This prevents attackers from bypassing client-side validation and injecting malicious data.
## 5. Testing Approaches
Testing is essential for ensuring the quality and reliability of Kivy applications.
### 5.1 Unit Testing
* **Test individual components:** Unit tests should focus on testing individual components, such as widgets, functions, and classes, in isolation.
* **Use a testing framework:** Use a testing framework, such as `unittest` or `pytest`, to organize and run your unit tests.
* **Write clear and concise tests:** Write clear and concise tests that are easy to understand and maintain.
* **Test edge cases:** Test edge cases to ensure that your code handles unexpected input or conditions correctly.
* **Use mock objects:** Use mock objects to isolate components from external dependencies, such as databases or network services.
* **Aim for high test coverage:** Aim for high test coverage to ensure that most of your code is tested.
* **Test Kivy properties:** Ensure your tests check for proper updates to Kivy Properties, including bindings and event triggering.
### 5.2 Integration Testing
* **Test interactions between components:** Integration tests should focus on testing the interactions between different components, such as screens, widgets, and data models.
* **Test the application as a whole:** Integration tests should also test the application as a whole to ensure that all components work together correctly.
* **Use a testing framework:** Use a testing framework, such as `unittest` or `pytest`, to organize and run your integration tests.
* **Use a GUI testing framework:** If you need to test the GUI directly, consider using a GUI testing framework, such as `kivy.tests` or `pytest-kivy`.
### 5.3 End-to-End Testing
* **Simulate user interactions:** End-to-end tests should simulate user interactions to test the application from the user's perspective.
* **Test the entire application flow:** End-to-end tests should test the entire application flow to ensure that all steps work correctly.
* **Use a testing framework:** Use a testing framework, such as `Selenium` or `Appium`, to automate your end-to-end tests.
* **Run tests on different platforms:** Run your end-to-end tests on different platforms to ensure that your application works correctly on all supported platforms.
### 5.4 Test Organization
* **Create a dedicated `tests` directory:** Create a dedicated `tests` directory to store your tests.
* **Organize tests by module:** Organize your tests by module to make it easier to find and run tests.
* **Use descriptive test names:** Use descriptive test names to make it clear what each test is testing.
* **Write test documentation:** Write test documentation to explain the purpose of each test and how it works.
* **Run tests automatically:** Run your tests automatically whenever you make changes to your code. Use a continuous integration system to automate this process.
### 5.5 Mocking and Stubbing
* **Use mock objects to isolate components:** Use mock objects to isolate components from external dependencies, such as databases or network services.
* **Use stub objects to simulate external dependencies:** Use stub objects to simulate external dependencies, such as APIs or hardware devices.
* **Use a mocking framework:** Use a mocking framework, such as `unittest.mock` or `mockito`, to create mock and stub objects easily.
* **Avoid over-mocking:** Avoid over-mocking, as this can make your tests less effective. Only mock dependencies that are truly external to the component being tested.
## 6. Common Pitfalls and Gotchas
Be aware of common pitfalls and gotchas when developing Kivy applications to avoid wasting time and effort.
### 6.1 Frequent Mistakes
* **Forgetting to call `super()`:** When subclassing Kivy widgets or classes, remember to call the `super()` method to initialize the parent class properly.
* **Not understanding Kivy's property system:** Kivy's property system is powerful but can be confusing at first. Make sure you understand how properties work and how to use them effectively.
* **Blocking the UI thread:** Avoid performing long-running tasks on the UI thread, as this can cause the application to freeze.
* **Not handling events correctly:** Handle events correctly to ensure that your application responds to user interactions as expected.
* **Not using the KV language effectively:** The KV language is a powerful tool for defining UI layouts and styles. Use it effectively to separate your UI definition from your Python code.
* **Incorrect file paths in deployment:** When packaging your app, ensure that file paths to assets and other dependencies are correct relative to the packaged application.
### 6.2 Edge Cases
* **Different screen resolutions:** Test your application on different screen resolutions to ensure that the UI scales correctly.
* **Different operating systems:** Test your application on different operating systems to ensure that it works correctly on all supported platforms.
* **Different hardware devices:** Test your application on different hardware devices to ensure that it works correctly on all target devices.
* **Handling orientation changes:** Implement proper handling of orientation changes (portrait/landscape) in your layouts.
* **Input from different devices:** Be aware of differences in input from touchscreens vs. mouse/keyboard, especially when designing interactions.
### 6.3 Version-Specific Issues
* **Kivy API changes:** Be aware of API changes between different versions of Kivy. Consult the Kivy documentation for migration guides.
* **Dependency compatibility:** Ensure that your dependencies are compatible with the version of Kivy you are using.
* **Deprecated features:** Be aware of deprecated features and avoid using them in new code. Migrate existing code to use the recommended alternatives.
### 6.4 Compatibility Concerns
* **Operating system dependencies:** Be aware of operating system dependencies and ensure that your application works correctly on all supported platforms.
* **Hardware dependencies:** Be aware of hardware dependencies and ensure that your application works correctly on all target devices.
* **Python version compatibility:** Ensure your code is compatible with supported Python versions. Kivy officially supports certain Python versions, so check the Kivy documentation for details.
### 6.5 Debugging Strategies
* **Use the Kivy console:** Use the Kivy console to inspect the state of your application and execute commands dynamically.
* **Use logging:** Use logging to record information about your application's behavior. This can be helpful for debugging issues that occur in production.
* **Use a debugger:** Use a debugger, such as `pdb` or `pycharm`, to step through your code and inspect variables.
* **Use Kivy's interactive mode:** Use Kivy's interactive mode to test UI layouts and styles without running the entire application.
* **Read Kivy's error messages carefully:** Kivy's error messages often provide valuable information about the cause of the error. Read them carefully and try to understand what they mean.
* **Search the Kivy documentation and online forums:** Search the Kivy documentation and online forums for solutions to common problems. The Kivy community is very active and helpful.
## 7. Tooling and Environment
Using the right tools and environment can significantly improve your Kivy development experience.
### 7.1 Recommended Development Tools
* **Text editor/IDE:** Use a good text editor or IDE, such as VS Code, PyCharm, or Sublime Text. These tools provide features such as syntax highlighting, code completion, and debugging.
* **Kivy Designer:** Kivy Designer is a visual UI designer that allows you to create UI layouts visually. It can be helpful for prototyping and experimenting with different UI designs.
* **Buildozer:** Buildozer is a tool for packaging Kivy applications for Android, iOS, and other platforms. It simplifies the process of creating deployment packages.
* **Python virtual environment manager:** Use virtualenv, venv (Python 3.3+), or conda to create isolated Python environments for your Kivy projects. This helps manage dependencies and avoid conflicts.
### 7.2 Build Configuration
* **Use a build system:** Use a build system, such as Make or CMake, to automate the build process.
* **Create a build script:** Create a build script to automate the build process. The build script should handle tasks such as installing dependencies, compiling code, and packaging the application.
* **Use environment variables:** Use environment variables to configure the build process. This allows you to customize the build process without modifying the code.
* **Use a configuration file:** Use a configuration file to store application settings. This allows you to change application settings without modifying the code.
### 7.3 Linting and Formatting
* **Use a linter:** Use a linter, such as `flake8` or `pylint`, to check your code for style errors and potential problems.
* **Use a code formatter:** Use a code formatter, such as `black` or `autopep8`, to automatically format your code according to PEP 8 guidelines.
* **Configure your editor/IDE:** Configure your editor/IDE to run the linter and code formatter automatically whenever you save a file. This helps ensure that your code is always clean and consistent.
### 7.4 Deployment
* **Choose the right deployment method:** Choose the right deployment method for your target platform. For Android, you can use Buildozer to create an APK. For desktop platforms, you can use PyInstaller or cx_Freeze to create executable files.
* **Create a deployment script:** Create a deployment script to automate the deployment process. The deployment script should handle tasks such as building the application, packaging it, and uploading it to the target platform.
* **Test your application thoroughly:** Test your application thoroughly on the target platform before deploying it to users.
* **Follow platform-specific guidelines:** Adhere to platform-specific guidelines for packaging and distributing applications (e.g., Google Play Store guidelines, Apple App Store guidelines).
### 7.5 CI/CD Integration
* **Use a CI/CD system:** Use a continuous integration/continuous delivery (CI/CD) system, such as Jenkins, Travis CI, GitHub Actions, or GitLab CI, to automate the build, test, and deployment process.
* **Create a CI/CD pipeline:** Create a CI/CD pipeline to automate the build, test, and deployment process. The pipeline should handle tasks such as checking out the code, installing dependencies, running tests, building the application, and deploying it to the target platform.
* **Use automated testing:** Use automated testing to ensure that your code is always working correctly. The CI/CD pipeline should run your tests automatically whenever you make changes to your code.
* **Automate deployment:** Automate deployment to the target platform. The CI/CD pipeline should deploy your application automatically whenever it passes all tests.
* **Integration with code review:** Integrate your CI/CD pipeline with your code review process. The CI/CD pipeline should run tests and linters automatically whenever a new pull request is created.
By following these best practices, you can create maintainable, efficient, secure, and testable Kivy applications.
│ └── fonts/
```
## 4. Use Kivy Properties for Reactive UI
**Opinion:** Always use Kivy's `Property` types (`StringProperty`, `NumericProperty`, `ObjectProperty`, `BooleanProperty`, `ListProperty`, `DictProperty`) for attributes that need to trigger UI updates or be bound in KV language. Avoid plain Python attributes for reactive data.
**When to use:** For any data that affects the UI or needs to be observed.
**❌ BAD (Plain Python attributes):**
```python
# my_widget.py
from kivy.uix.boxlayout import BoxLayout
class MyWidget(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.my_text = "Initial Text" # Will not update UI automatically
```
```kv
# my_widget.kv
<MyWidget>:
Label:
text: root.my_text # Won't update if root.my_text changes in Python
```
**✅ GOOD (Kivy Properties):**
```python
# my_widget.py
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
class MyWidget(BoxLayout):
my_text = StringProperty("Initial Text") # Will update UI automatically
def update_text(self, new_text):
self.my_text = new_text
```
```kv
# my_widget.kv
<MyWidget>:
orientation: 'vertical'
Label:
text: root.my_text # Updates automatically
Button:
text: "Change Text"
on_release: root.update_text("New Text from Button")
```
## 5. Adhere to Kivy Coding Style
**Opinion:** Follow PEP 8 with Kivy-specific adjustments for consistency and readability.
**When to use:** Always.
- **Docstrings/Comments:** Limit to 79 characters.
- **Quotes:** Use single quotes for identifiers (e.g., `'horizontal'`, `'center_x'`). Use double quotes for user-facing strings (e.g., `"Hello World"`). Escape characters as needed.
**❌ BAD (Inconsistent quoting, long comments):**
```python
# Bad example
def my_function(orientation="vertical"): # This is a very long comment that exceeds the recommended line length for docstrings and comments in Kivy projects, making the code harder to read and maintain.
print(f"Orientation is {orientation}")
```
**✅ GOOD (Kivy style):**
```python
# Good example
def my_function(orientation='vertical'):
"""
Prints the given orientation.
"""
print(f"Orientation is {orientation}")
# In KV
Button:
text: "Submit" # User-facing string
orientation: 'horizontal' # Identifier
```
## 6. Manage Widget IDs Effectively
**Opinion:** Use `ids` in KV for referencing child widgets within the same KV scope. For cross-scope communication or complex state management, bind to Kivy Properties or use event dispatching. Avoid over-reliance on `ids` in Python code for direct widget manipulation, especially across different screens or deeply nested structures.
**When to use:**
- `ids`: For simple interactions within a single KV file or custom widget's KV.
- Kivy Properties/Events: For robust communication between Python logic and UI, or between different components.
**❌ BAD (Over-reliance on `ids` in Python, hard-to-trace dependencies):**
```python
# main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class HomeScreen(Screen):
pass
class MyManager(ScreenManager):
pass
class BadApp(App):
def build(self):
return Builder.load_string("""
MyManager:
HomeScreen:
name: 'home'
BoxLayout:
Button:
id: my_button
text: 'Press Me'
""")
def on_start(self):
# Fragile: relies on deep knowledge of widget tree and IDs
self.root.get_screen('home').ids.my_button.text = "Updated!"
BadApp().run()
```
**✅ GOOD (Using Kivy Properties for state, `ids` for local KV binding):**
```python
# main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty
class HomeScreen(Screen):
button_text = StringProperty("Press Me")
class MyManager(ScreenManager):
pass
class GoodApp(App):
def build(self):
return Builder.load_string("""
MyManager:
HomeScreen:
name: 'home'
BoxLayout:
Button:
text: root.button_text # Bind to property
on_release: app.update_home_button()
""")
def update_home_button(self):
home_screen = self.root.get_screen('home')
home_screen.button_text = "Updated via Property!"
GoodApp().run()
```
## 7. Optimize Layouts for Responsiveness
**Opinion:** Use `BoxLayout`, `GridLayout`, and `FloatLayout` (or their KivyMD equivalents like `MDBoxLayout`) for responsive UI design. Understand their strengths and weaknesses.
**When to use:**
- `BoxLayout` / `MDBoxLayout`: For linear arrangements (horizontal/vertical). Excellent for toolbars, navigation, or stacking elements.
- `GridLayout` / `MDGridLayout`: For grid-based arrangements where items have similar sizes.
- `FloatLayout` / `MDFloatLayout`: For absolute positioning or layering widgets, often combined with `pos_hint` and `size_hint`. Use sparingly for main layouts, prefer for overlays or specific components.
**❌ BAD (Hardcoded sizes/positions, non-responsive):**
```kv
# my_screen.kv
FloatLayout:
Button:
size: 100, 50 # Fixed size
pos: 50, 50 # Fixed position
Label:
text: "Hello"
size: 200, 30
pos: 150, 100
```
**✅ GOOD (Responsive layouts with hints):**
```kv
# my_screen.kv
MDBoxLayout:
orientation: 'vertical'
MDTopAppBar:
title: "Responsive App"
MDBoxLayout:
orientation: 'horizontal'
spacing: dp(10)
padding: dp(10)
MDButton:
MDButtonText:
text: "Button 1"
size_hint_x: 0.3 # Takes 30% of horizontal space
MDLabel:
text: "Content Area"
halign: 'center'
size_hint_x: 0.7 # Takes 70% of horizontal space
```
## 8. Implement Type Hints
**Opinion:** Use Python type hints (`typing` module) for all Kivy Python code. This improves code readability, enables static analysis, and reduces runtime errors.
**When to use:** For function arguments, return values, and class attributes (especially Kivy Properties).
**❌ BAD (Untyped Python):**
```python
# my_widget.py
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
class MyWidget(BoxLayout):
my_text = StringProperty("Initial Text")
def update_text(self, new_text):
self.my_text = new_text
```
**✅ GOOD (Typed Python):**
```python
# my_widget.py
from typing import Union
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, ObjectProperty
from kivy.event import EventDispatcher
class MyWidget(BoxLayout):
my_text: StringProperty = StringProperty("Initial Text")
# Type hint for a Kivy Property that holds an instance of another widget
linked_widget: ObjectProperty = ObjectProperty(None)
def update_text(self, new_text: str) -> None:
self.my_text = new_text
def set_linked_widget(self, widget: EventDispatcher) -> None:
self.linked_widget = widget
```
## 9. Performance Considerations
**Opinion:** Profile your KV language parsing with `KIVY_PROFILE_LANG=1` during development. Avoid excessive nesting or complex calculations in KV where Python might be more efficient. Use `RecycleView` for large lists.
**When to use:**
- `KIVY_PROFILE_LANG`: During development and optimization phases.
- `RecycleView`: For displaying large, dynamic lists of items to maintain smooth scrolling performance.
**❌ BAD (Slow list rendering):**
```kv
# my_screen.kv
ScrollView:
BoxLayout: # Creates all widgets at once, even if not visible
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
# ... many, many labels or buttons ...
```
**✅ GOOD (Efficient list rendering with RecycleView):**
```kv
# my_screen.kv
#:import Factory kivy.factory.Factory # Needed if ListItem is not defined in Python
<ListItem@BoxLayout>: # Define a view class for RecycleView
size_hint_y: None
height: dp(48)
Label:
id: rv_label
text: root.text_item
RecycleView:
id: rv
viewclass: 'ListItem'
RecycleBoxLayout:
default_size: None, dp(48)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
```
```python
# main.py
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout # Needed for ListItem base class
class ListItem(BoxLayout): # Define the view class in Python
text_item = StringProperty('')
class RecycleViewApp(MDApp):
def build(self):
return Builder.load_string("""
#:import ListItem __main__.ListItem # Import the Python class
MDBoxLayout:
orientation: 'vertical'
MDTopAppBar:
title: "RecycleView Example"
RecycleView:
id: rv
viewclass: 'ListItem'
RecycleBoxLayout:
default_size: None, dp(48)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
""")
def on_start(self):
# Populate RecycleView with data
data = [{'text_item': f'Item {i}'} for i in range(1000)]
self.root.ids.rv.data = data
RecycleViewApp().run()
```
## 10. Accessibility Considerations
**Opinion:** While Kivy's direct accessibility features are evolving, ensure your UI is navigable and understandable for all users. Provide clear labels, sufficient contrast, and logical focus order.
**When to use:** In all UI design and implementation.
- **Labels:** Always provide clear, descriptive text for interactive elements.
- **Contrast:** Ensure sufficient color contrast between text and backgrounds.
- **Focus Order:** Design layouts that allow for a logical tab/focus order, even if Kivy doesn't fully automate it. Consider adding custom keyboard navigation if needed.
**❌ BAD (Ambiguous UI):**
```kv
# my_screen.kv
Button:
text: '' # Empty text, relies on icon only
Image:
source: 'info_icon.png'
```
**✅ GOOD (Clear labels, descriptive text):**
```kv
# my_screen.kv
MDButton:
MDButtonText:
text: "Show Info" # Clear text label
MDButtonIcon:
icon: "information-outline"
on_release: app.show_info_dialog()
```