0
# Redis CLI Integration
1
2
Redis command-line interface integration providing an interactive Redis console within Flask-Admin. This allows administrators to execute Redis commands directly through the web interface.
3
4
## Capabilities
5
6
### Redis CLI View
7
8
Interactive Redis console that provides a web-based interface for executing Redis commands with safety checks and formatted output.
9
10
```python { .api }
11
from flask_admin.contrib.rediscli import RedisCli
12
13
class RedisCli(BaseView):
14
def __init__(
15
self,
16
redis,
17
name=None,
18
category=None,
19
endpoint=None,
20
url=None
21
):
22
"""
23
Redis CLI view for web-based Redis command execution.
24
25
Parameters:
26
- redis: Redis connection object
27
- name: View name (optional)
28
- category: View category for menu organization
29
- endpoint: Base endpoint name
30
- url: Base URL path
31
"""
32
```
33
34
### Redis Command Execution
35
36
Built-in command parsing, validation, and execution system with error handling and result formatting.
37
38
```python { .api }
39
def _execute_command(self, name, args):
40
"""
41
Execute Redis command with validation and error handling.
42
43
Parameters:
44
- name: str, Redis command name
45
- args: tuple, Command arguments
46
47
Returns:
48
Formatted HTML response with command results
49
"""
50
51
def _parse_cmd(self, cmd):
52
"""
53
Parse command string using shell-like syntax.
54
55
Parameters:
56
- cmd: str, Raw command string to parse
57
58
Returns:
59
tuple: Parsed command parts
60
"""
61
```
62
63
### Command Safety and Remapping
64
65
Command filtering and remapping system to prevent dangerous operations and provide user-friendly aliases.
66
67
```python { .api }
68
# Command configuration attributes
69
remapped_commands = {
70
'del': 'delete'
71
}
72
73
excluded_commands = set(('pubsub', 'set_response_callback', 'from_url'))
74
```
75
76
### Built-in Help System
77
78
Interactive help system providing command documentation and usage information.
79
80
```python { .api }
81
def _cmd_help(self, *args):
82
"""
83
Built-in help command implementation.
84
85
Parameters:
86
- args: Optional command name for specific help
87
88
Returns:
89
Help text for specified command or general help
90
"""
91
```
92
93
### Error Handling
94
95
Comprehensive error handling with user-friendly error messages and exception management.
96
97
```python { .api }
98
class CommandError(Exception):
99
"""Redis CLI command error exception."""
100
101
def _error(self, msg):
102
"""
103
Format error message as HTML response.
104
105
Parameters:
106
- msg: str, Error message to format
107
108
Returns:
109
HTML formatted error response
110
"""
111
```
112
113
## Usage Examples
114
115
### Basic Redis CLI Setup
116
117
```python
118
from flask import Flask
119
from flask_admin import Admin
120
from flask_admin.contrib.rediscli import RedisCli
121
import redis
122
123
app = Flask(__name__)
124
admin = Admin(app)
125
126
# Create Redis connection
127
redis_client = redis.Redis(host='localhost', port=6379, db=0)
128
129
# Add Redis CLI to admin interface
130
admin.add_view(RedisCli(
131
redis_client,
132
name='Redis Console',
133
category='Tools'
134
))
135
```
136
137
### Custom Redis CLI with Authentication
138
139
```python
140
import redis
141
from flask_admin.contrib.rediscli import RedisCli
142
143
# Redis with authentication
144
redis_client = redis.Redis(
145
host='localhost',
146
port=6379,
147
password='your-password',
148
decode_responses=True
149
)
150
151
class SecureRedisCli(RedisCli):
152
def is_accessible(self):
153
# Add your security checks here
154
return current_user.is_admin()
155
156
admin.add_view(SecureRedisCli(
157
redis_client,
158
name='Redis Admin',
159
category='Database Tools'
160
))
161
```