mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 14:52:55 +08:00
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:
+411
-66
@@ -1,74 +1,419 @@
|
||||
---
|
||||
description: This rule provides guidelines and best practices for developing maintainable, performant, and secure Streamlit applications. It covers code organization, performance optimization, security considerations, testing strategies, and common pitfalls to avoid.
|
||||
globs: *.py
|
||||
description: Enforce modern, performant, and maintainable Streamlit development practices, focusing on caching, modularity, and robust dependency management.
|
||||
globs: **/*.py
|
||||
---
|
||||
- **Code Organization and Structure**:
|
||||
- **Directory Structure**: Structure your Streamlit application using a modular directory structure to improve maintainability and collaboration. A suggested layout includes folders for pages, services, models, components, and utilities.
|
||||
|
||||
my_streamlit_app/
|
||||
├── app.py # Main entry point for the Streamlit app
|
||||
├── pages/ # Directory for individual app pages
|
||||
│ ├── dashboard.py # Dashboard page
|
||||
│ ├── reports.py # Reports page
|
||||
│ └── settings.py # Settings page
|
||||
├── services/ # Directory for business logic and data operations
|
||||
│ ├── data_loader.py # Handles data loading
|
||||
│ ├── analysis.py # Contains data analysis functions
|
||||
│ └── utils.py # Utility functions
|
||||
├── models/ # Directory for data models and schemas
|
||||
│ └── data_model.py # Defines data structures
|
||||
├── components/ # Directory for reusable UI components
|
||||
│ ├── button.py # Custom button component
|
||||
│ └── chart.py # Custom chart component
|
||||
├── utils/ # Miscellaneous utility functions
|
||||
│ └── helpers.py # Helper functions
|
||||
├── requirements.txt # List of Python dependencies
|
||||
├── .gitignore # Specifies intentionally untracked files that Git should ignore
|
||||
└── .venv/ # Python virtual environment (optional)
|
||||
|
||||
- **File Naming Conventions**: Use descriptive and consistent file names for your Streamlit components, services, and pages. For example, `data_loader.py`, `dashboard.py`, and `button.py`.
|
||||
- **Module Organization**: Organize your Streamlit application into logical modules to promote code reuse and reduce complexity. Create separate modules for data loading, data processing, UI components, and utility functions. Use relative imports to maintain a clear module structure.
|
||||
- **Component Architecture**: Build reusable UI components using Streamlit's `st.components.v1` API or custom functions. Encapsulate component logic and styling to promote consistency and maintainability. Use props or parameters to customize component behavior and appearance.
|
||||
- **Code Splitting**: Split large Streamlit applications into multiple pages using the `st.page_config` and `st.switch_page` functions. This enhances navigation and reduces initial loading times. Consider lazy loading strategies using the `secrets` feature to control which parts of the application are loaded when.
|
||||
# Streamlit Best Practices
|
||||
|
||||
- **Common Patterns and Anti-patterns**:
|
||||
- **Design Patterns**: Employ the Observer pattern for reactive UIs, Facade pattern for simplifying complex interactions, and Strategy pattern to manage different data sources or algorithms.
|
||||
- **Recommended Approaches**: Utilize Streamlit's `st.form` for handling user input and submission. Use `st.session_state` to manage application state across reruns. Leverage Streamlit's caching mechanisms (`@st.cache_data`, `@st.cache_resource`) to optimize performance.
|
||||
- **Anti-patterns**: Avoid performing expensive computations or data loading operations directly within the main Streamlit application loop. Do not store large datasets in `st.session_state`. Avoid using global variables, as they can lead to unexpected behavior.
|
||||
- **State Management**: Use `st.session_state` for storing and managing application state across reruns. Initialize session state variables using a conditional statement (`if 'key' not in st.session_state:`) to prevent overwriting values on subsequent reruns. Consider implementing a state management class or module to encapsulate and manage complex state logic.
|
||||
- **Error Handling**: Implement comprehensive error handling throughout your Streamlit application. Use `try...except` blocks to catch exceptions and display informative error messages to the user using `st.error` or `st.exception`. Log errors to a file or monitoring system for debugging and analysis.
|
||||
This guide outlines the definitive best practices for building robust, performant, and maintainable Streamlit applications. Adhere to these rules to ensure your apps are efficient, scalable, and easy to collaborate on.
|
||||
|
||||
- **Performance Considerations**:
|
||||
- **Optimization Techniques**: Use caching (`@st.cache_data`, `@st.cache_resource`) to store the results of expensive computations or data loading operations. Optimize data loading by using appropriate data formats (e.g., Parquet, Feather) and filtering data at the source. Debounce user interactions to reduce the frequency of reruns.
|
||||
- **Memory Management**: Avoid loading large datasets into memory unnecessarily. Use data streaming techniques or chunking to process large datasets in smaller portions. Delete unused variables and data structures to release memory.
|
||||
- **Rendering Optimization**: Use Streamlit's built-in rendering optimizations, such as delta generation, to minimize the amount of data that needs to be sent to the browser. Avoid creating complex layouts with deeply nested components, as this can impact rendering performance.
|
||||
- **Bundle Size**: Minimize external dependencies to reduce the bundle size and improve loading times. Use a `.streamlitignore` file to exclude unnecessary files and directories from the Streamlit deployment bundle. Consider using a CDN to serve static assets.
|
||||
- **Lazy Loading**: Implement lazy loading for expensive components or sections of your Streamlit application. Load components only when they are needed, using conditional rendering or callbacks. Utilize the `secrets` functionality to control the conditional loading of certain modules if access to a given secret is available.
|
||||
## 1. Code Organization and Structure
|
||||
|
||||
- **Security Best Practices**:
|
||||
- **Common Vulnerabilities**: Be aware of common vulnerabilities, such as cross-site scripting (XSS), SQL injection, and command injection. Prevent these vulnerabilities by implementing proper input validation and sanitization, and by avoiding the execution of untrusted code.
|
||||
- **Input Validation**: Validate all user inputs to prevent malicious code from being injected into your Streamlit application. Use regular expressions or custom validation functions to ensure that inputs conform to expected formats and ranges. Sanitize user inputs by encoding or escaping special characters.
|
||||
- **Authentication and Authorization**: Implement authentication and authorization mechanisms to control access to sensitive data and functionality. Use Streamlit's `secrets` API to store API keys, passwords, and other sensitive information securely. Consider using a third-party authentication provider, such as Auth0 or Google Identity Platform.
|
||||
- **Data Protection**: Protect sensitive data by encrypting it at rest and in transit. Use HTTPS to secure communication between the browser and the Streamlit application. Implement data masking or redaction to prevent sensitive data from being displayed to unauthorized users.
|
||||
- **Secure API Communication**: Always use HTTPS for API communications. Validate the origin and authenticity of data from external APIs. Protect API keys using Streamlit secrets and environment variables.
|
||||
Always separate UI logic from data processing and business logic. Modularize your application into distinct files or directories.
|
||||
|
||||
- **Testing Approaches**:
|
||||
- **Unit Testing**: Unit test individual Streamlit components, services, and utility functions using the `pytest` framework. Mock external dependencies to isolate the code being tested. Write test cases that cover a range of inputs and edge cases.
|
||||
- **Integration Testing**: Integrate test Streamlit applications to verify that components and modules work together correctly. Use Streamlit's testing utilities to simulate user interactions and verify the output.
|
||||
- **End-to-end Testing**: Perform end-to-end tests to verify that the entire Streamlit application works as expected. Use Selenium or Cypress to automate browser interactions and verify the UI and functionality.
|
||||
- **Test Organization**: Organize your tests into a separate `tests` directory within your Streamlit project. Use descriptive test names and docstrings to explain the purpose of each test. Follow a consistent naming convention for test files and functions.
|
||||
- **Mocking and Stubbing**: Use mocking and stubbing techniques to isolate the code being tested and to simulate external dependencies. Use the `unittest.mock` module or a third-party mocking library to create mock objects and stubs.
|
||||
### 1.1 Project Structure
|
||||
|
||||
- **Common Pitfalls and Gotchas**:
|
||||
- **Frequent Mistakes**: Forgetting to use `@st.cache_data` or `@st.cache_resource` for expensive operations, not managing `st.session_state` properly, and neglecting error handling are common mistakes.
|
||||
- **Edge Cases**: Be aware of edge cases, such as handling missing data, dealing with invalid inputs, and managing concurrent user sessions.
|
||||
- **Version-Specific Issues**: Check the Streamlit documentation and release notes for version-specific issues and compatibility concerns. Be aware of breaking changes when upgrading to a newer version of Streamlit.
|
||||
- **Compatibility Concerns**: Ensure that your Streamlit application is compatible with the browsers and operating systems that your users will be using. Test your application on different devices and browsers to identify and resolve compatibility issues.
|
||||
- **Debugging Strategies**: Use Streamlit's debugging tools, such as the debugger and the console, to identify and resolve issues. Print debug messages to the console to track the execution flow and to inspect variables. Use `st.experimental_rerun` to programmatically trigger reruns during debugging.
|
||||
Organize your project for clarity and maintainability.
|
||||
|
||||
- **Tooling and Environment**:
|
||||
- **Recommended Tools**: Use VS Code with the Python extension for development. Use `pip` or `uv` for dependency management, and Docker for containerization.
|
||||
- **Build Configuration**: Create a `requirements.txt` file to list your Streamlit application's dependencies. Use a virtual environment to isolate your project's dependencies from the system-level Python installation. Consider using a build tool, such as Make or Poetry, to automate the build process.
|
||||
- **Linting and Formatting**: Use linters and formatters, such as `flake8` and `black`, to enforce code style guidelines and to identify potential errors. Configure your editor to automatically run linters and formatters on save.
|
||||
- **Deployment**: Deploy your Streamlit application to Streamlit Cloud, Heroku, AWS, or another cloud platform. Use Docker to containerize your application and to ensure consistent deployment across different environments.
|
||||
- **CI/CD Integration**: Integrate your Streamlit project with a CI/CD pipeline to automate the testing, building, and deployment processes. Use GitHub Actions, GitLab CI, or another CI/CD platform to configure your pipeline.
|
||||
❌ BAD: Monolithic `app.py`
|
||||
```python
|
||||
# app.py
|
||||
import streamlit as st
|
||||
import pandas as pd
|
||||
|
||||
def load_and_process_data(file_path):
|
||||
df = pd.read_csv(file_path)
|
||||
# ... complex processing ...
|
||||
return df
|
||||
|
||||
st.title("My Data App")
|
||||
uploaded_file = st.file_uploader("Upload CSV")
|
||||
if uploaded_file:
|
||||
data = load_and_process_data(uploaded_file)
|
||||
st.dataframe(data)
|
||||
# ... more UI and logic ...
|
||||
```
|
||||
|
||||
✅ GOOD: Modularized Structure
|
||||
```
|
||||
my_streamlit_app/
|
||||
├── app.py # Main entry point, orchestrates pages
|
||||
├── pages/
|
||||
│ └── dashboard_page.py # Specific page UI and logic
|
||||
├── services/
|
||||
│ └── data_service.py # Data loading and processing functions
|
||||
├── components/
|
||||
│ └── custom_widgets.py # Reusable UI components
|
||||
├── requirements.txt
|
||||
└── .gitignore
|
||||
```
|
||||
|
||||
`app.py`:
|
||||
```python
|
||||
# app.py
|
||||
import streamlit as st
|
||||
from pages import dashboard_page # Assuming dashboard_page.py has a main() function
|
||||
|
||||
st.set_page_config(layout="wide")
|
||||
|
||||
# Example of multi-page app structure
|
||||
PAGES = {
|
||||
"Dashboard": dashboard_page,
|
||||
# "Another Page": another_page_module,
|
||||
}
|
||||
|
||||
with st.sidebar:
|
||||
st.title('Navigation')
|
||||
selection = st.radio("Go to", list(PAGES.keys()))
|
||||
|
||||
page = PAGES[selection]
|
||||
page.main() # Each page module must have a main() function
|
||||
```
|
||||
|
||||
`pages/dashboard_page.py`:
|
||||
```python
|
||||
# pages/dashboard_page.py
|
||||
import streamlit as st
|
||||
from services.data_service import load_data, process_data
|
||||
|
||||
def main():
|
||||
st.title("Dashboard Overview")
|
||||
|
||||
uploaded_file = st.file_uploader("Upload CSV", type=["csv"])
|
||||
if uploaded_file:
|
||||
raw_df = load_data(uploaded_file)
|
||||
st.write("Raw Data:")
|
||||
st.dataframe(raw_df.head())
|
||||
|
||||
processed_df = process_data(raw_df)
|
||||
st.write("Processed Data:")
|
||||
st.dataframe(processed_df)
|
||||
|
||||
# Further UI elements specific to the dashboard
|
||||
```
|
||||
|
||||
`services/data_service.py`:
|
||||
```python
|
||||
# services/data_service.py
|
||||
import pandas as pd
|
||||
import streamlit as st
|
||||
from typing import Union
|
||||
|
||||
@st.cache_data
|
||||
def load_data(file_path: Union[str, bytes]) -> pd.DataFrame:
|
||||
"""Loads data from a file-like object or path."""
|
||||
return pd.read_csv(file_path)
|
||||
|
||||
@st.cache_data
|
||||
def process_data(df: pd.DataFrame) -> pd.DataFrame:
|
||||
"""Performs complex data processing."""
|
||||
# Example: clean, transform, aggregate
|
||||
processed_df = df.dropna().copy()
|
||||
processed_df['new_col'] = processed_df['value'] * 2 # Example transformation
|
||||
return processed_df
|
||||
```
|
||||
|
||||
## 2. Performance Considerations
|
||||
|
||||
Streamlit reruns the entire script on every interaction. Efficient caching and state management are critical.
|
||||
|
||||
### 2.1 Caching Expensive Operations
|
||||
|
||||
Always cache functions that load data, perform heavy computations, or interact with external resources. Use `st.cache_data` for serializable data and `st.cache_resource` for unserializable objects like database connections or ML models.
|
||||
|
||||
❌ BAD: Uncached data loading
|
||||
```python
|
||||
import streamlit as st
|
||||
import pandas as pd
|
||||
|
||||
def get_large_dataset():
|
||||
# Simulates a slow data load
|
||||
return pd.read_csv("https://example.com/large_data.csv")
|
||||
|
||||
df = get_large_dataset() # Runs on every interaction
|
||||
st.dataframe(df)
|
||||
st.button("Rerun App")
|
||||
```
|
||||
|
||||
✅ GOOD: Cached data loading
|
||||
```python
|
||||
import streamlit as st
|
||||
import pandas as pd
|
||||
|
||||
@st.cache_data
|
||||
def get_large_dataset() -> pd.DataFrame:
|
||||
"""Loads a large dataset from a URL."""
|
||||
# Simulates a slow data load
|
||||
return pd.read_csv("https://example.com/large_data.csv")
|
||||
|
||||
df = get_large_dataset() # Only runs once per unique URL/function code
|
||||
st.dataframe(df)
|
||||
st.button("Rerun App")
|
||||
```
|
||||
|
||||
### 2.2 Session State for User-Specific Data
|
||||
|
||||
Use `st.session_state` to store and persist user-specific variables across reruns. This is essential for interactive elements where state must be maintained.
|
||||
|
||||
❌ BAD: Global variables or re-initializing state
|
||||
```python
|
||||
import streamlit as st
|
||||
|
||||
if 'counter' not in st.session_state:
|
||||
counter = 0 # This will reset on every rerun!
|
||||
|
||||
st.session_state.counter = counter # Incorrect assignment
|
||||
|
||||
if st.button("Increment"):
|
||||
st.session_state.counter += 1 # This will fail if counter is not in session_state initially
|
||||
|
||||
st.write(f"Counter: {st.session_state.counter}")
|
||||
```
|
||||
|
||||
✅ GOOD: Proper `st.session_state` initialization and usage
|
||||
```python
|
||||
import streamlit as st
|
||||
|
||||
# Initialize state only if it doesn't exist
|
||||
if 'counter' not in st.session_state:
|
||||
st.session_state.counter = 0
|
||||
|
||||
st.write(f"Counter: {st.session_state.counter}")
|
||||
|
||||
if st.button("Increment"):
|
||||
st.session_state.counter += 1
|
||||
|
||||
# Example for input widgets
|
||||
name = st.text_input("Enter your name", key="user_name_input")
|
||||
st.write(f"Hello, {st.session_state.user_name_input}!")
|
||||
```
|
||||
|
||||
## 3. Common Patterns and Anti-patterns
|
||||
|
||||
### 3.1 Separate UI from Logic
|
||||
|
||||
Keep Streamlit UI calls (`st.write`, `st.button`, etc.) distinct from core business logic.
|
||||
|
||||
❌ BAD: Intermingled UI and logic
|
||||
```python
|
||||
import streamlit as st
|
||||
|
||||
def calculate_and_display(a, b):
|
||||
result = a + b
|
||||
st.write(f"The sum is: {result}") # UI call inside logic
|
||||
|
||||
num1 = st.number_input("First number", value=1)
|
||||
num2 = st.number_input("Second number", value=2)
|
||||
calculate_and_display(num1, num2)
|
||||
```
|
||||
|
||||
✅ GOOD: Clear separation
|
||||
```python
|
||||
import streamlit as st
|
||||
|
||||
def calculate_sum(a: int, b: int) -> int:
|
||||
"""Calculates the sum of two numbers."""
|
||||
return a + b
|
||||
|
||||
num1 = st.number_input("First number", value=1)
|
||||
num2 = st.number_input("Second number", value=2)
|
||||
|
||||
sum_result = calculate_sum(num1, num2)
|
||||
st.write(f"The sum is: {sum_result}") # UI call outside logic
|
||||
```
|
||||
|
||||
### 3.2 Use Context Managers for Layout
|
||||
|
||||
Leverage `st.sidebar`, `st.columns`, `st.expander`, `st.container`, and `st.tabs` as context managers for cleaner layout code.
|
||||
|
||||
❌ BAD: Manual layout with repeated elements
|
||||
```python
|
||||
import streamlit as st
|
||||
|
||||
col1, col2 = st.columns(2)
|
||||
col1.write("Column 1 content")
|
||||
col2.write("Column 2 content")
|
||||
|
||||
st.sidebar.header("Sidebar")
|
||||
st.sidebar.write("Sidebar content")
|
||||
```
|
||||
|
||||
✅ GOOD: Context managers for structured layout
|
||||
```python
|
||||
import streamlit as st
|
||||
|
||||
with st.sidebar:
|
||||
st.header("App Controls")
|
||||
option = st.selectbox("Choose an option", ["A", "B", "C"])
|
||||
|
||||
col1, col2 = st.columns(2)
|
||||
with col1:
|
||||
st.subheader("Data Input")
|
||||
st.write(f"Selected option: {option}")
|
||||
# ... more content ...
|
||||
|
||||
with col2:
|
||||
st.subheader("Visualization")
|
||||
st.line_chart([1, 2, 3])
|
||||
# ... more content ...
|
||||
```
|
||||
|
||||
## 4. Common Pitfalls and Gotchas
|
||||
|
||||
### 4.1 Modifying Cached Objects
|
||||
|
||||
`st.cache_data` returns a *copy* of the cached object, preventing unintended mutations. `st.cache_resource` returns the *original* object, so be cautious with mutations.
|
||||
|
||||
❌ BAD: Modifying `st.cache_resource` return value without care
|
||||
```python
|
||||
import streamlit as st
|
||||
|
||||
@st.cache_resource
|
||||
def get_shared_list():
|
||||
return [1, 2, 3]
|
||||
|
||||
my_list = get_shared_list()
|
||||
st.write(f"Original list: {my_list}")
|
||||
|
||||
if st.button("Append to list"):
|
||||
my_list.append(4) # This modifies the cached object for ALL users/sessions!
|
||||
st.write("Appended 4")
|
||||
|
||||
st.write(f"Current list: {my_list}")
|
||||
```
|
||||
|
||||
✅ GOOD: Treat `st.cache_resource` objects as immutable or explicitly copy
|
||||
```python
|
||||
import streamlit as st
|
||||
|
||||
@st.cache_resource
|
||||
def get_shared_list():
|
||||
return [1, 2, 3]
|
||||
|
||||
# Option 1: Treat as immutable
|
||||
st.write(f"Shared list: {get_shared_list()}")
|
||||
|
||||
# Option 2: Create a local copy if modification is needed
|
||||
my_list_copy = list(get_shared_list())
|
||||
st.write(f"Original copy: {my_list_copy}")
|
||||
|
||||
if st.button("Append to list"):
|
||||
my_list_copy.append(4) # Only modifies the copy for this session
|
||||
st.write("Appended 4")
|
||||
|
||||
st.write(f"Current copy: {my_list_copy}")
|
||||
```
|
||||
|
||||
### 4.2 Widget Keys
|
||||
|
||||
Always provide unique `key` arguments for widgets that appear multiple times or whose state needs to be explicitly managed. This prevents unexpected behavior when widgets are dynamically rendered or reordered.
|
||||
|
||||
❌ BAD: Missing or duplicate keys
|
||||
```python
|
||||
import streamlit as st
|
||||
|
||||
for i in range(3):
|
||||
st.checkbox(f"Option {i}") # Keys will be implicitly generated, might conflict
|
||||
|
||||
st.text_input("Enter value")
|
||||
st.text_input("Enter another value") # These will implicitly get the same key if not specified
|
||||
```
|
||||
|
||||
✅ GOOD: Unique and descriptive keys
|
||||
```python
|
||||
import streamlit as st
|
||||
|
||||
for i in range(3):
|
||||
st.checkbox(f"Option {i}", key=f"checkbox_{i}")
|
||||
|
||||
st.text_input("Enter value", key="first_input")
|
||||
st.text_input("Enter another value", key="second_input")
|
||||
```
|
||||
|
||||
## 5. Type Hints
|
||||
|
||||
Always use type hints for function signatures and complex variables. This improves code readability, enables static analysis, and aids collaboration.
|
||||
|
||||
❌ BAD: Untyped function
|
||||
```python
|
||||
def process_data(data):
|
||||
# What is 'data'? A list, DataFrame, dict?
|
||||
return len(data)
|
||||
```
|
||||
|
||||
✅ GOOD: Type-hinted function
|
||||
```python
|
||||
import pandas as pd
|
||||
from typing import List, Dict, Union
|
||||
|
||||
def process_data(data: Union[pd.DataFrame, List[Dict]]) -> int:
|
||||
"""Processes data and returns its length."""
|
||||
if isinstance(data, pd.DataFrame):
|
||||
return len(data)
|
||||
elif isinstance(data, list):
|
||||
return len(data)
|
||||
else:
|
||||
raise TypeError("Unsupported data type")
|
||||
```
|
||||
|
||||
## 6. Packaging and Dependencies
|
||||
|
||||
Manage your dependencies rigorously using `requirements.txt` for Python packages and `packages.txt` for system-level dependencies.
|
||||
|
||||
### 6.1 Python Dependencies (`requirements.txt`)
|
||||
|
||||
Pin exact versions or use clear version constraints.
|
||||
|
||||
❌ BAD: Vague `requirements.txt`
|
||||
```
|
||||
pandas
|
||||
streamlit
|
||||
matplotlib
|
||||
```
|
||||
|
||||
✅ GOOD: Specific `requirements.txt`
|
||||
```
|
||||
streamlit==1.52.1
|
||||
pandas>=2.1.0,<3.0.0
|
||||
matplotlib~=3.8.0
|
||||
```
|
||||
|
||||
### 6.2 System Dependencies (`packages.txt`)
|
||||
|
||||
If your app requires non-Python libraries (e.g., for image processing, database connectors), list them in `packages.txt` for Streamlit Community Cloud.
|
||||
|
||||
`packages.txt` example:
|
||||
```
|
||||
build-essential
|
||||
libgl1-mesa-glx
|
||||
```
|
||||
|
||||
## 7. Testing Approaches
|
||||
|
||||
While Streamlit apps are primarily UI-driven, you must test your underlying logic.
|
||||
|
||||
### 7.1 Unit Testing Core Logic
|
||||
|
||||
Isolate and unit test all non-UI functions (e.g., data processing, calculations, API calls). Use standard Python testing frameworks like `pytest`.
|
||||
|
||||
`test_data_service.py`:
|
||||
```python
|
||||
# tests/test_data_service.py
|
||||
import pandas as pd
|
||||
from services.data_service import process_data
|
||||
|
||||
def test_process_data_dropna():
|
||||
df = pd.DataFrame({'value': [1, 2, None, 4]})
|
||||
processed_df = process_data(df)
|
||||
assert len(processed_df) == 3
|
||||
assert 'new_col' in processed_df.columns
|
||||
assert processed_df['new_col'].iloc[0] == 2
|
||||
|
||||
def test_process_data_empty_df():
|
||||
df = pd.DataFrame({'value': []})
|
||||
processed_df = process_data(df)
|
||||
assert processed_df.empty
|
||||
```
|
||||
|
||||
### 7.2 End-to-End Testing (Optional, for complex apps)
|
||||
|
||||
For complex applications, consider tools like `Playwright` or `Selenium` to simulate user interactions and verify UI behavior. However, prioritize robust unit tests for core logic first, as E2E testing adds significant overhead.
|
||||
Reference in New Issue
Block a user