Skip to content

Commit 75925fb

Browse files
author
Xing Han Lu
authored
Merge pull request #126 from plotly/try-to-add-async
Add an option to generate async component
2 parents c59567c + 84eb6e8 commit 75925fb

File tree

14 files changed

+295
-80
lines changed

14 files changed

+295
-80
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Created by .ignore support plugin (hsz.mobi)
2+
### Webpack
3+
.build_cache
24
### VisualStudioCode template
35
.vscode/*
46
!.vscode/settings.json

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ to generate the components in the `build:backends` script of the generated
4141
`package.json`.
4242
4343
44+
## Advanced customization
45+
46+
### Shared cache groups for async chunks
47+
48+
Shared async chunks for code that repeats across multiple async chunks is already supported through our custom `webpack.config.js` optimizations. You can leverage it by manually the path of `{{cookiecutter.project_shortname}}-shared.js` to `_js_dist` inside `{{cookiecutter.project_shortname}}/__init__.py` (as well as the associated external URL).
49+
4450
## More Resources
4551
4652
- Found a bug or have a feature request? [Create an issue](https://github.com/plotly/dash-component-boilerplate/issues/new)

cookiecutter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"author_email": "Enter your email (For package.json)",
99
"github_org": "",
1010
"description": "Project Description",
11+
"use_async": ["False", "True"],
1112
"license": [
1213
"MIT License",
1314
"BSD License",

hooks/post_gen_project.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import shlex
44
import sys
55
import os
6+
import shutil
67
import subprocess
78

89
install_deps = '{{cookiecutter.install_dependencies}}'
910
project_shortname = '{{cookiecutter.project_shortname}}'
11+
use_async = '{{cookiecutter.use_async}}'
1012

1113

1214
is_windows = sys.platform == 'win32'
@@ -33,6 +35,22 @@ def _execute_command(cmd):
3335
return status
3436

3537

38+
39+
# Remove the cookiecutter_templates directory since it only contains
40+
# files that are conditionally included.
41+
template_dir = os.path.join(os.getcwd(), 'cookiecutter_templates')
42+
shutil.rmtree(template_dir)
43+
44+
print("\n\n\nuse_async")
45+
print(use_async)
46+
# If it doesn't use async, we can remove the fragments and lazyloader.js
47+
if use_async != "True":
48+
print('use_async is set to False, your component will not be lazy loaded and fragments will not be created.')
49+
shutil.rmtree(os.path.join(os.getcwd(), 'src', 'lib', 'fragments'))
50+
os.remove(os.path.join(os.getcwd(), 'src', 'lib', 'LazyLoader.js'))
51+
52+
53+
3654
if install_deps != 'True':
3755
print('`install_dependencies` is false!!', file=sys.stderr)
3856
print('Please create a venv in your project root'
@@ -90,4 +108,5 @@ def _execute_command(cmd):
90108

91109
print('\n{} ready!\n'.format(project_shortname))
92110

111+
93112
sys.exit(0)

{{cookiecutter.project_shortname}}/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Created by .ignore support plugin (hsz.mobi)
2+
### Webpack
3+
.build_cache
24
### VisualStudioCode template
35
.vscode/*
46
!.vscode/settings.json

{{cookiecutter.project_shortname}}/MANIFEST.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
include {{cookiecutter.project_shortname}}/{{cookiecutter.project_shortname}}.min.js
22
include {{cookiecutter.project_shortname}}/{{cookiecutter.project_shortname}}.min.js.map
3+
include {{cookiecutter.project_shortname}}/async-*.js
4+
include {{cookiecutter.project_shortname}}/async-*.js.map
5+
include {{cookiecutter.project_shortname}}/*-shared.js
6+
include {{cookiecutter.project_shortname}}/*-shared.js.map
37
include {{cookiecutter.project_shortname}}/metadata.json
48
include {{cookiecutter.project_shortname}}/package-info.json
59
include README.md
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React, {Component} from 'react';
2+
import PropTypes from 'prop-types';
3+
import { {{cookiecutter.component_name}} as RealComponent } from '../LazyLoader';
4+
5+
/**
6+
* ExampleComponent is an example component.
7+
* It takes a property, `label`, and
8+
* displays it.
9+
* It renders an input with the property `value`
10+
* which is editable by the user.
11+
*/
12+
export default class {{cookiecutter.component_name}} extends Component {
13+
render() {
14+
return (
15+
<React.Suspense fallback={null}>
16+
<RealComponent {...this.props}/>
17+
</React.Suspense>
18+
);
19+
}
20+
}
21+
22+
{{cookiecutter.component_name}}.defaultProps = {};
23+
24+
{{cookiecutter.component_name}}.propTypes = {
25+
/**
26+
* The ID used to identify this component in Dash callbacks.
27+
*/
28+
id: PropTypes.string,
29+
30+
/**
31+
* A label that will be printed when this component is rendered.
32+
*/
33+
label: PropTypes.string.isRequired,
34+
35+
/**
36+
* The value displayed in the input.
37+
*/
38+
value: PropTypes.string,
39+
40+
/**
41+
* Dash-assigned callback that should be called to report property changes
42+
* to Dash, to make them available for callbacks.
43+
*/
44+
setProps: PropTypes.func
45+
};
46+
47+
48+
export const defaultProps = {{cookiecutter.component_name}}.defaultProps;
49+
export const propTypes = {{cookiecutter.component_name}}.propTypes;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import React, {Component} from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
/**
5+
* ExampleComponent is an example component.
6+
* It takes a property, `label`, and
7+
* displays it.
8+
* It renders an input with the property `value`
9+
* which is editable by the user.
10+
*/
11+
export default class {{cookiecutter.component_name}} extends Component {
12+
render() {
13+
const {id, label, setProps, value} = this.props;
14+
15+
return (
16+
<div id={id}>
17+
ExampleComponent: {label}&nbsp;
18+
<input
19+
value={value}
20+
onChange={
21+
/*
22+
* Send the new value to the parent component.
23+
* setProps is a prop that is automatically supplied
24+
* by dash's front-end ("dash-renderer").
25+
* In a Dash app, this will update the component's
26+
* props and send the data back to the Python Dash
27+
* app server if a callback uses the modified prop as
28+
* Input or State.
29+
*/
30+
e => setProps({ value: e.target.value })
31+
}
32+
/>
33+
</div>
34+
);
35+
}
36+
}
37+
38+
{{cookiecutter.component_name}}.defaultProps = {};
39+
40+
{{cookiecutter.component_name}}.propTypes = {
41+
/**
42+
* The ID used to identify this component in Dash callbacks.
43+
*/
44+
id: PropTypes.string,
45+
46+
/**
47+
* A label that will be printed when this component is rendered.
48+
*/
49+
label: PropTypes.string.isRequired,
50+
51+
/**
52+
* The value displayed in the input.
53+
*/
54+
value: PropTypes.string,
55+
56+
/**
57+
* Dash-assigned callback that should be called to report property changes
58+
* to Dash, to make them available for callbacks.
59+
*/
60+
setProps: PropTypes.func
61+
};

{{cookiecutter.project_shortname}}/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
"@babel/plugin-proposal-object-rest-spread": "^7.5.4",
3434
"@babel/preset-env": "^7.5.4",
3535
"@babel/preset-react": "^7.0.0",
36+
"@plotly/webpack-dash-dynamic-import": "^1.2.0",
37+
"@plotly/dash-component-plugins": "^1.2.0",
3638
"babel-eslint": "^10.0.2",
3739
"babel-loader": "^8.0.6",
3840
"copyfiles": "^2.1.1",
@@ -48,6 +50,7 @@
4850
"react-dom": "^16.8.6",
4951
"styled-jsx": "^3.2.1",
5052
"style-loader": "^0.23.1",
53+
"terser-webpack-plugin": "^2.3.0",
5154
"webpack": "4.36.1",
5255
"webpack-cli": "3.3.6",
5356
"webpack-serve": "3.1.0"

{{cookiecutter.project_shortname}}/src/lib/LazyLoader.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)