0
# Core Application
1
2
The Dash class is the central application instance that manages server configuration, component layout, callback registration, and application lifecycle. It provides the foundation for building reactive web applications with Python.
3
4
## Capabilities
5
6
### Application Initialization
7
8
Creates and configures a Dash application with server integration, asset management, and plugin support.
9
10
```python { .api }
11
class Dash:
12
def __init__(
13
self,
14
name: str = None,
15
server: Any = None,
16
assets_folder: str = 'assets',
17
pages_folder: str = 'pages',
18
use_pages: bool = None,
19
assets_url_path: str = '/assets/',
20
assets_ignore: str = '',
21
external_scripts: List[Union[str, Dict]] = None,
22
external_stylesheets: List[Union[str, Dict]] = None,
23
suppress_callback_exceptions: bool = None,
24
prevent_initial_callbacks: bool = None,
25
show_undo_redo: bool = False,
26
plugins: List = None,
27
title: str = 'Dash',
28
update_title: str = 'Updating...',
29
meta_tags: List[Dict[str, str]] = None,
30
index_string: str = None,
31
compress: bool = None,
32
**kwargs
33
):
34
"""
35
Initialize a Dash application.
36
37
Parameters:
38
- name: Application name for Flask (typically __name__)
39
- server: Existing Flask server instance
40
- assets_folder: Directory for static assets
41
- pages_folder: Directory for page modules (multi-page apps)
42
- use_pages: Enable automatic page discovery
43
- external_scripts: External JavaScript resources
44
- external_stylesheets: External CSS resources
45
- suppress_callback_exceptions: Allow callbacks for components not in layout
46
- prevent_initial_callbacks: Prevent callbacks on initial load
47
- plugins: List of Dash plugins
48
- title: Browser window title
49
- meta_tags: HTML meta tags for SEO
50
"""
51
```
52
53
### Layout Management
54
55
Defines the application's user interface through a hierarchical component tree.
56
57
```python { .api }
58
@property
59
def layout(self) -> LayoutType:
60
"""Get the application layout."""
61
62
@layout.setter
63
def layout(self, value: LayoutType):
64
"""Set the application layout."""
65
66
def serve_layout(self) -> LayoutType:
67
"""Serve dynamic layout (called on each page load)."""
68
```
69
70
### Server Operations
71
72
Controls application server lifecycle including development and production modes.
73
74
```python { .api }
75
def run(
76
self,
77
host: str = '127.0.0.1',
78
port: int = 8050,
79
proxy: str = None,
80
debug: bool = None,
81
dev_tools_ui: bool = None,
82
dev_tools_props_check: bool = None,
83
dev_tools_serve_dev_bundles: bool = None,
84
dev_tools_hot_reload: bool = None,
85
dev_tools_hot_reload_interval: int = None,
86
dev_tools_hot_reload_watch_interval: int = None,
87
dev_tools_hot_reload_max_retry: int = None,
88
dev_tools_silence_routes_logging: bool = None,
89
dev_tools_prune_errors: bool = None,
90
**flask_run_options
91
):
92
"""
93
Run the Dash application server.
94
95
Parameters:
96
- host: Server host address
97
- port: Server port number
98
- debug: Enable debug mode with hot reloading
99
- dev_tools_ui: Show dev tools UI
100
- dev_tools_hot_reload: Enable hot reloading
101
"""
102
103
def run_server(self, *args, **kwargs):
104
"""Alias for run() method."""
105
```
106
107
### Callback Registration
108
109
Registers reactive callback functions that respond to component interactions.
110
111
```python { .api }
112
def callback(
113
self,
114
output: Union[Output, List[Output]],
115
inputs: Union[Input, List[Input]] = None,
116
state: Union[State, List[State]] = None,
117
prevent_initial_call: bool = None,
118
background: bool = None,
119
manager: Any = None,
120
**kwargs
121
) -> Callable:
122
"""
123
Decorator for registering callback functions.
124
125
Parameters:
126
- output: Output dependencies (what gets updated)
127
- inputs: Input dependencies (what triggers the callback)
128
- state: State dependencies (additional values without triggering)
129
- prevent_initial_call: Prevent callback on initial load
130
- background: Run callback in background thread
131
"""
132
133
def clientside_callback(
134
self,
135
clientside_function: Union[ClientsideFunction, str],
136
output: Union[Output, List[Output]],
137
inputs: Union[Input, List[Input]] = None,
138
state: Union[State, List[State]] = None,
139
prevent_initial_call: bool = None,
140
**kwargs
141
):
142
"""
143
Register JavaScript callback for client-side execution.
144
145
Parameters:
146
- clientside_function: JavaScript function reference
147
- output: Output dependencies
148
- inputs: Input dependencies
149
- state: State dependencies
150
"""
151
```
152
153
### Configuration Properties
154
155
Application-level configuration and metadata management.
156
157
```python { .api }
158
@property
159
def config(self) -> AttributeDict:
160
"""Get application configuration."""
161
162
@property
163
def scripts(self) -> Scripts:
164
"""Get JavaScript resources."""
165
166
@property
167
def css(self) -> Css:
168
"""Get CSS resources."""
169
170
@property
171
def index_string(self) -> str:
172
"""Get HTML index template."""
173
174
@index_string.setter
175
def index_string(self, value: str):
176
"""Set HTML index template."""
177
178
@property
179
def title(self) -> str:
180
"""Get application title."""
181
182
@title.setter
183
def title(self, value: str):
184
"""Set application title."""
185
```
186
187
### Asset and Resource Management
188
189
Handles static assets, external resources, and URL generation.
190
191
```python { .api }
192
def get_asset_url(self, path: str) -> str:
193
"""Generate URL for asset file."""
194
195
def get_relative_path(self, path: str) -> str:
196
"""Get relative path from current request."""
197
198
def strip_relative_path(self, path: str) -> str:
199
"""Remove relative path prefix."""
200
```
201
202
## Usage Examples
203
204
### Basic Application
205
206
```python
207
from dash import Dash, html
208
209
app = Dash(__name__)
210
211
app.layout = html.Div([
212
html.H1("My Dash App"),
213
html.P("Hello World!")
214
])
215
216
if __name__ == '__main__':
217
app.run(debug=True)
218
```
219
220
### Application with External Resources
221
222
```python
223
from dash import Dash, html
224
225
external_stylesheets = [
226
'https://codepen.io/chriddyp/pen/bWLwgP.css'
227
]
228
229
external_scripts = [
230
'https://code.jquery.com/jquery-3.6.0.min.js'
231
]
232
233
app = Dash(
234
__name__,
235
external_stylesheets=external_stylesheets,
236
external_scripts=external_scripts,
237
title="My Custom App"
238
)
239
240
app.layout = html.Div("Content")
241
app.run(debug=True)
242
```
243
244
### Custom Server Integration
245
246
```python
247
import flask
248
from dash import Dash, html
249
250
server = flask.Flask(__name__)
251
252
@server.route('/api/data')
253
def serve_data():
254
return {'message': 'Hello from Flask'}
255
256
app = Dash(__name__, server=server)
257
app.layout = html.Div("Dash with custom Flask routes")
258
259
if __name__ == '__main__':
260
app.run(debug=True)
261
```
262
263
## Types
264
265
```python { .api }
266
LayoutType = Union[Component, List, Dict, str, int, float]
267
ServerType = Any # Flask server instance
268
AssetList = List[Union[str, Dict[str, str]]]
269
MetaTagList = List[Dict[str, str]]
270
PluginList = List[Any]
271
AttributeDict = Dict[str, Any]
272
```