Skip to content

Commit 12cb45e

Browse files
authored
Merge pull request #87 from Python-Fuzzylogic/copilot/fix-17
Add Simple GUI for Fuzzy Logic Experimentation and Code Generation
2 parents 8ad56e9 + 7db5a15 commit 12cb45e

File tree

9 files changed

+1131
-1
lines changed

9 files changed

+1131
-1
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,47 @@ To make it possible to write fuzzy logic in the most pythonic and simplest way i
2424
* Domain and Set uses an assignment trick to make it possible to instantiate Set() without passing domain and name over and over (yet still be explicit, just not the way one would normally expect). This also allows to call sets as Domain.attributes, which also normally shouldn't be possible (since they are technically not attributes). However, this allows interesting things like dangling sets (sets without domains) that can be freely combined with other sets to avoid cluttering of domain-namespaces and just have the resulting set assigned to a domain to work with.
2525

2626
# Installation
27+
```
28+
pip install fuzzylogic
29+
```
30+
31+
Note: If you want to use the experimental GUI, you'll also need matplotlib:
32+
```
33+
pip install matplotlib
34+
```
35+
36+
# GUI for Experimentation
37+
38+
The library now includes a web-based GUI for experimenting with fuzzy logic and generating code! This makes it easy to:
39+
40+
- Visually create and test fuzzy logic systems
41+
- Experiment with different membership functions
42+
- Generate Python code for your fuzzy logic setup
43+
- Plot and visualize fuzzy sets
44+
45+
## Starting the GUI
46+
47+
```python
48+
import fuzzylogic
49+
50+
# Start the GUI (opens browser automatically)
51+
fuzzylogic.run_gui()
52+
53+
# Or from command line
54+
# python -m fuzzylogic.gui.cli
55+
```
56+
57+
![Fuzzy Logic GUI Screenshot](https://github.com/user-attachments/assets/3c3c4de2-0caf-4e29-9623-576be5c9a93b)
58+
59+
## GUI Features
60+
61+
- **Create Domains**: Define fuzzy logic domains with custom ranges
62+
- **Add Fuzzy Sets**: Create sets using R (rising), S (falling), triangular, trapezoid, and rectangular functions
63+
- **Visualization**: Real-time plotting of fuzzy sets
64+
- **Test Values**: Interactive testing of input values
65+
- **Code Generation**: Automatic Python code generation
66+
67+
# Documentation
2768
Just enter
2869
`python -m pip install fuzzylogic`
2970
in a commandline prompt and you should be good to go!

src/fuzzylogic/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
__version__ = (1, 5, 0)
1+
__version__ = (1, 5, 0)
2+
3+
def run_gui(port=8000):
4+
"""Start the fuzzy logic experimentation GUI.
5+
6+
Args:
7+
port: Port to run the web server on (default: 8000)
8+
"""
9+
try:
10+
from .gui.app import run_gui as _run_gui
11+
_run_gui(port)
12+
except ImportError as e:
13+
print(f"GUI dependencies not available: {e}")
14+
print("Please install matplotlib if you haven't already: pip install matplotlib")

src/fuzzylogic/gui/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Fuzzy Logic GUI
2+
3+
This directory contains a web-based GUI for experimenting with fuzzy logic and generating Python code.
4+
5+
## Features
6+
7+
- **Create Domains**: Define fuzzy logic domains with custom ranges and resolution
8+
- **Add Fuzzy Sets**: Create fuzzy sets using various membership functions:
9+
- R (Rising): Sigmoid-like function that rises from 0 to 1
10+
- S (Falling): Sigmoid-like function that falls from 1 to 0
11+
- Triangular: Triangle-shaped membership function
12+
- Trapezoid: Trapezoid-shaped membership function
13+
- Rectangular: Rectangular/plateau membership function
14+
- **Visualization**: Plot domains and their fuzzy sets using matplotlib
15+
- **Test Values**: Test input values against all sets in a domain
16+
- **Code Generation**: Generate Python code that recreates your fuzzy logic setup
17+
18+
## Usage
19+
20+
### Command Line
21+
22+
Start the GUI from the command line:
23+
24+
```bash
25+
# From the repository root
26+
python -m fuzzylogic.gui.cli
27+
28+
# Or with custom port
29+
python -m fuzzylogic.gui.cli --port 8080
30+
31+
# Don't open browser automatically
32+
python -m fuzzylogic.gui.cli --no-browser
33+
```
34+
35+
### Python API
36+
37+
Start the GUI programmatically:
38+
39+
```python
40+
import fuzzylogic
41+
42+
# Start the GUI (will open browser automatically)
43+
fuzzylogic.run_gui()
44+
45+
# Or with custom port
46+
fuzzylogic.run_gui(port=8080)
47+
```
48+
49+
### Direct Module Usage
50+
51+
```python
52+
from fuzzylogic.gui.app import run_gui
53+
54+
# Start the server
55+
run_gui(port=8000)
56+
```
57+
58+
## Example Workflow
59+
60+
1. **Create a Domain**: Enter a name (e.g., "temperature"), set the range (0-40), and click "Create Domain"
61+
62+
2. **Add Fuzzy Sets**:
63+
- Select the domain from the dropdown
64+
- Enter a set name (e.g., "cold")
65+
- Choose function type (e.g., "S" for falling)
66+
- Set parameters (e.g., low=0, high=15)
67+
- Click "Add Set"
68+
69+
3. **Visualize**: Select the domain and click "Plot Domain" to see a graph of all fuzzy sets
70+
71+
4. **Test Values**: Enter a test value and see the membership degrees for each set
72+
73+
5. **Generate Code**: Click "Generate Python Code" to get Python code that recreates your setup
74+
75+
## Implementation Details
76+
77+
The GUI is implemented as a simple HTTP server that serves a single-page web application. It uses:
78+
79+
- Pure Python with built-in `http.server` (no external web framework dependencies)
80+
- Matplotlib for plotting (with Agg backend for server-side rendering)
81+
- HTML/CSS/JavaScript for the frontend
82+
- JSON API for communication between frontend and backend
83+
84+
## Files
85+
86+
- `app.py`: Main GUI application with web server and fuzzy logic interface
87+
- `cli.py`: Command-line interface for starting the GUI
88+
- `__init__.py`: Module initialization

src/fuzzylogic/gui/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""GUI module for fuzzy logic experimentation."""

src/fuzzylogic/gui/__main__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Main entry point for the fuzzy logic GUI command.
4+
"""
5+
6+
from fuzzylogic.gui.cli import main
7+
8+
if __name__ == '__main__':
9+
main()

0 commit comments

Comments
 (0)