Skip to content

Commit 75d484d

Browse files
author
Xing Han Lu
authored
Create CONTRIBUTING.md
Former-commit-id: 3e426bd
1 parent fcb1db3 commit 75d484d

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed

CONTRIBUTING.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# CONTRIBUTING
2+
3+
## Running an example app after cloning the repo
4+
5+
You will need to run applications, and specify filenames, from the
6+
root directory of the repository. e.g., if the name of the app you
7+
want to run is `my_dash_app` and the app filename is `app.py`, you
8+
would need to run `python apps/my_dash_app/app.py` from the root
9+
of the repository.
10+
11+
Each app has a requirements.txt, install the dependencies in a virtual
12+
environment.
13+
14+
15+
## Contributing to the sample apps repo
16+
17+
_"DDS app" below refers to the deployed application. For example, if
18+
the deployment will eventually be hosted at
19+
https://dash-gallery.plotly.host/my-dash-app, "DDS app name" is
20+
`my-dash-app`._
21+
22+
### Adding a new app
23+
24+
Create an app on Dash Playground. This will be the location of the
25+
auto-deployment. To do this, log into the app manager on
26+
[dash-playground.plotly.host](https://dash-playground.plotly.host)
27+
and click "initialize app".
28+
29+
Create a branch from `master` to work on your app, the name is not required
30+
to be anything specific. Switch to this branch, then navigate to the `apps/`
31+
directory and add a directory for your app.
32+
33+
There are two options when you are naming the folder:
34+
35+
1. Make the folder have the _exact same_ name as the Dash app name.
36+
37+
2. (Python apps only) Select any other name, but _update the file
38+
[`apps_mapping.py`](apps_directory_mapping.py)_ with the Dash app
39+
name and the folder name you have selected.
40+
41+
Navigate to the directory you just created, and write a small README
42+
that only contains the name of the app. Stage the README and commit it
43+
to your branch.
44+
45+
See [project boilerplate!](https://github.com/plotly/dash-sample-apps#project-boilerplate)
46+
47+
### Notes on adding a new Dash for R app
48+
49+
Contributing an app written with Dash for R is very similar to the steps outlined above.
50+
51+
1. Make the folder have the _exact same_ name as the Dash app name.
52+
53+
2. Ensure that the file containing your app code is named `app.R`.
54+
55+
3. The `Procfile` should contain
56+
57+
```
58+
web: R -f /app/app.R
59+
```
60+
61+
4. Routing and request pathname prefixes should be set. One approach might be to include
62+
63+
```
64+
appName <- Sys.getenv("DASH_APP_NAME")
65+
pathPrefix <- sprintf("/%s/", appName)
66+
67+
Sys.setenv(DASH_ROUTES_PATHNAME_PREFIX = pathPrefix,
68+
DASH_REQUESTS_PATHNAME_PREFIX = pathPrefix)
69+
```
70+
71+
at the head of your `app.R` file.
72+
73+
5. `run_server()` should be provided the host and port information explicitly, e.g.
74+
75+
``
76+
app$run_server(host = "0.0.0.0", port = Sys.getenv('PORT', 8050))
77+
``
78+
79+
### Making changes to an existing app
80+
81+
Create a new branch - of any name - for your code changes.
82+
Then, navigate to the directory that has the same name as
83+
the DDS app.
84+
85+
When you are finished, make a pull request from your branch to the master
86+
branch. Once you have passed your code review, you can merge your PR.
87+
88+
## Dash app project structure
89+
90+
#### Data
91+
- All data (csv, json, txt, etc) should be in a data folder
92+
- `/apps/{DASH_APP_NAME}/data/`
93+
94+
#### Assets
95+
- All stylesheets and javascript should be in an assets folder
96+
- `/apps/{DASH_APP_NAME}/assets/`
97+
98+
#### These files will still need to be present within the app folder.
99+
100+
- **`Procfile`** gets run at root level for deployment
101+
- Make sure python working directory is at the app level
102+
- Ex. `web: gunicorn app:server`
103+
- **`requirements.txt`**
104+
- Install project dependecies in a virtual environment
105+
106+
#### Project boilerplate
107+
108+
apps
109+
├── ...
110+
├── {DASH_APP_NAME} # app project level
111+
│ ├── assets/ # all stylesheets and javascript files
112+
│ ├── data/ # all data (csv, json, txt, etc)
113+
│ ├── app.py # dash application entry point
114+
│ ├── Procfile # used for heroku deployment (how to run app)
115+
│ ├── requirements.txt # project dependecies
116+
│ └── ...
117+
└── ...
118+
119+
#### Handle relative path
120+
121+
Assets should never use a relative path, as this will fail when deployed to Dash Enterprise due to use of subdirectories for serving apps.
122+
123+
Reading from assets and data folder
124+
```Python
125+
Img(src="./assets/logo.png") will fail at root level
126+
```
127+
128+
Tips
129+
130+
- Use [get_asset_url()](https://dash.plot.ly/dash-deployment-server/static-assets)
131+
- Use [Pathlib](https://docs.python.org/3/library/pathlib.html) for more flexibility
132+
133+
```Python
134+
import pathlib
135+
import pandas as pd
136+
137+
# get relative assets
138+
html.Img(src=app.get_asset_url('logo.png')) # /assets/logo.png
139+
140+
# get relative data
141+
142+
DATA_PATH = pathlib.Path(__file__).parent.joinpath("data") # /data
143+
df = pd.read_csv(DATA_PATH.joinpath("sample-data.csv")) # /data/sample-data.csv
144+
145+
with open(DATA_PATH.joinpath("sample-data.csv")) as f: # /data/sample-data.csv
146+
some_string = f.read()
147+
```
148+
149+
## Developer Guide
150+
151+
#### Creating a new project
152+
153+
```
154+
# branch off master
155+
git checkout -b "{YOUR_CUSTOM_BRANCH}"
156+
157+
# create a new folder in apps/
158+
mkdir /apps/{DASH_APP_NAME}
159+
160+
# push new branch
161+
git push -u origin {YOUR_CUSTOM_BRANCH}
162+
```
163+
164+
#### Before committing
165+
166+
```
167+
# make sure your code is linted (we use black)
168+
black . --exclude=venv/ --check
169+
170+
# if black is not installed
171+
pip install black
172+
```
173+
174+
175+
#### App is ready to go!
176+
```
177+
# once your branch is ready, make a PR into master!
178+
179+
PR has two checkers.
180+
1. make sure your code passed the black linter
181+
2. make sure your project is deployed on dns playground
182+
```
183+

0 commit comments

Comments
 (0)