|
1 | 1 | # Contributing to Notebooks |
2 | 2 |
|
3 | | -# Notebooks |
4 | | - |
5 | | -## Names |
6 | | - |
7 | | -Notebook filenames cannot have spaces. Use underscores instead. This is |
8 | | -because pytest does not like spaces in command line arguments. |
9 | | - |
10 | | -## Style |
11 | | - |
12 | | -For maximum portability, accessibility, and ease of use, Notebooks in this |
13 | | -repository should not use a custom style or theme. |
14 | | - |
15 | | - |
16 | | -## Dependencies |
17 | | - |
18 | | -When a new notebook has a dependency that is not yet supported by the Docker image, |
19 | | -a new Docker image must be built. Additionally, add the new dependency to the |
20 | | -[imports_test notebook](dev/imports_test.ipynb). |
21 | | - |
22 | | -## Planet Data |
23 | | - |
24 | | -It is the intention of this notebook repository that a user be able to easily |
25 | | -determine the permissions needed within the Planet ecosystem to be able to run |
26 | | -all notebooks successfully. Therefore, we standardize and track Planet data |
27 | | -that are used across all notebooks within this repository. Right now, this applies |
28 | | -to Planet Imagery in the form of Areas of Interest and to Planet |
29 | | -Analytic Feeds in the form of suscription IDs. |
30 | | - |
31 | | -### Imagery - Area of Interest |
32 | | - |
33 | | -The first choice of area of interest (AOI) for any notebook is an AOI that is |
34 | | -already used in this repository. The geojson description of these AOIs is given |
35 | | -in [aois.geojson](dev/imports_test.ipynb) (it is easy to visualize these |
36 | | -aois directly in GitHub or by copy/pasting into [geojson.io](geojson.io). |
37 | | -These AOIs are also given in the |
38 | | -[repository_aois notebook](dev/repository_aois.ipynb). It is good practice |
39 | | -to add your notebook to the list of notebooks using each AOI. |
40 | | - |
41 | | -If the AOI for a notebook cannot be satisfied by the the AOIs already in use |
42 | | -in the repository, then email <[email protected]> so that we can consider |
43 | | -expanding our demo data coverage to include a new AOI. If the new AOI is included, |
44 | | -add the AOI to the |
45 | | -[repository_aois notebook](dev/repository_aois.ipynb). Run that notebook |
46 | | -through to the end to update [aois.geojson](dev/imports_test.ipynb). |
47 | | - |
48 | | -### Analytics Feed - Subscription ID |
49 | | - |
50 | | -The fist choice of Analytics Feed subscription id for any notebooks is a |
51 | | -subscription ID that is already used in this repository. The subscription IDs |
52 | | -used in this repository are maintained in the |
53 | | -[analygics_feeds notebook](analytics_feeds.ipynb). This notebook also helpfully |
54 | | -pulls the feed information from these subscription IDs. It is good practice |
55 | | -to add your notebook to the list of notebooks using each subscription ID. |
56 | | - |
57 | | -Currently, subscription IDs are not being tracked in any demo permissions program. |
58 | | -However, this may change in the future. If the subscription ID for a notebook |
59 | | -cannot be satisfied by the subscription IDs already in the repository, add |
60 | | -the subscription ID to the notebook and email <[email protected]> to notify us |
61 | | -of the change. (**NOTE**: this is likely to change to a request when demo |
62 | | -permissions for the analytics feed are established). |
63 | | - |
64 | | -## Notebook Validation |
65 | | - |
66 | | -To enable validation of the Docker image, every notebook should run successfully |
67 | | -when run from the command line. For notebooks where that just is not possible, |
68 | | -the notebooks can be excluded from automated running by adding its path to |
69 | | -[tests/skip_notebooks](tests/skip_notebooks). Excluding a notebook from automated running |
70 | | -means that it is excluded from Docker Image validation. **If a notebook is |
71 | | -skipped, it will not be guaranteed to be supported by the Docker image.** |
72 | | - |
73 | | -# Docker Image |
74 | | - |
75 | | -## Validation |
76 | | - |
77 | | -**Every time** the Docker image is changed, at the very least ensure that the |
78 | | -python packages still import without error by running the |
79 | | -[imports_test notebook](dev/imports_test.ipynb). |
80 | | - |
81 | | -It is also strongly recommended that you ensure the Docker image can run all |
82 | | -of the notebooks in the repository. This can be accomplished by automatically |
83 | | -running all of the notebooks using the supplied test script. To run the test script, |
84 | | -run the notebook in interactive mode, achieved by adding `/bin/bash` to |
85 | | -the container run command, e.g. |
86 | | -```bash |
87 | | -docker run -it --rm -p 8888:8888 \ |
88 | | - -v $PWD:/home/jovyan/work \ |
89 | | - -e PL_API_KEY='[YOUR-API-KEY]' \ |
90 | | - planet-notebooks /bin/bash |
| 3 | +Contributions are welcome to this repository. Please open a PR if you would like to contribute a notebook and we will review. Alternatively, if you have a request or an idea, you can [open an issue](https://github.com/planetlabs/notebooks/issues/new/choose). |
| 4 | + |
| 5 | +## Notebook Style |
| 6 | + |
| 7 | +If you are contributing a notebook, we've gathered these style guide requirements. |
| 8 | + |
| 9 | +### Filemames |
| 10 | + |
| 11 | +Notebook filenames should not have spaces. Please use underscores with `snake_case` instead for all files in the repository. |
| 12 | + |
| 13 | +Your filename should align to the title of the notebook. For example, a notebook titled **Generate Agriculture Index Time Series** should be `generate_agriculture_index_time_series.ipynb`. |
| 14 | + |
| 15 | +### HTML and Markdown |
| 16 | + |
| 17 | +For maximum portability, accessibility, and ease of use, notebooks in this repository should not use a custom style or theme. Please stick to standard markdown features so that it works across all environments. |
| 18 | + |
| 19 | +### Dependencies |
| 20 | + |
| 21 | +When a new notebook has a dependency that is not yet supported by the Docker image, please add the dependency to the [Docker setup requirements file](planet-notebook-docker/requirements.txt). |
| 22 | + |
| 23 | +Alternatively, you can choose to add package installation instructions in your Notebook, particularly if the notebook you are adding has any heavy, unique, or tricky dependencies. If you do this, make sure to include comments in the notebooks. |
| 24 | + |
| 25 | +### Ordering Notebooks |
| 26 | + |
| 27 | +If you are working on a multi-part guide that has several notebooks, prefix the notebooks with `1_`, `2_`, `3_`, and so on to indicate the order. |
| 28 | + |
| 29 | +### Optimize Cell Outputs for Viewing in the Browser |
| 30 | + |
| 31 | +If you have a cell that prints a very large json repsonse, consider alternative ways to view the data. Could it be viewed in a table or on a map? Very large json repsonses make notebooks difficult to read. |
| 32 | + |
| 33 | +### Use Colab |
| 34 | + |
| 35 | +When possible, add the option to open the notebook in Colab. This should be added as the very first line in the first cell of the Jupyter Notebook file so that it appears at the very top. |
| 36 | + |
| 37 | +For example: |
| 38 | + |
| 39 | +``` |
| 40 | +[](https://colab.research.google.com/github//planetlabs/notebooks/blob/master/jupyter-notebooks/workflows/planet_sandbox_data/agriculture-index-time-series/agriculture-index-time-series.ipynb) |
| 41 | +``` |
| 42 | + |
| 43 | +If your notebook includes Colab link, make sure that the packages used within it are either a) default colab packages or b) imported using a magic command `%pip install planet`. |
| 44 | + |
| 45 | +### Introduction |
| 46 | + |
| 47 | +For a notebook, please make sure to include context and description at the top using this format: |
| 48 | + |
| 49 | +``` |
| 50 | +<colab link> |
| 51 | +# Notebook Title |
| 52 | +
|
| 53 | +Information about the notebook |
| 54 | +
|
| 55 | +## Requirements |
| 56 | +
|
| 57 | +Any special requirements, set up instructions, permissions, pre-steps, etc. |
| 58 | +``` |
| 59 | + |
| 60 | +### Authentication |
| 61 | + |
| 62 | +Most Notebooks will likely require authenticating with Planet APIs. In order to use consistent authentication and schemes across all of the notebooks, we've provided 3 authentication snippets that we recommend you use: |
| 63 | + |
| 64 | +#### For Notebooks that use the Planet SDK for Python |
| 65 | + |
| 66 | +``` |
| 67 | +import planet |
| 68 | +
|
| 69 | +# If you are not already logged in, this will prompt you to open a web browser to log in. |
| 70 | +auth = planet.Auth.from_profile('planet-user', save_state_to_storage=True) |
| 71 | +if not auth.is_initialized(): |
| 72 | + auth.user_login(allow_open_browser=False, allow_tty_prompt=True) |
| 73 | +
|
| 74 | +session = planet.Session(auth) |
| 75 | +pl = planet.Planet(session) |
| 76 | +``` |
| 77 | + |
| 78 | +#### For Notebooks that use the Sentinel Hub Python SDK |
| 79 | + |
| 80 | +``` |
| 81 | +from sentinelhub import SHConfig |
| 82 | +
|
| 83 | +# Authenticate with the Sentinel Hub Python SDK; See docs: https://sentinelhub-py.readthedocs.io/en/latest/configure.html and https://docs.planet.com/develop/authentication |
| 84 | +# If no default configuration detected, enter a client ID and secret to authenticate. These can be obtained by creating an OAuth client here: https://insights.planet.com/account |
| 85 | +config = SHConfig() |
| 86 | +if not config.sh_client_id or not config.sh_client_secret: |
| 87 | + from getpass import getpass |
| 88 | + print('No credentials found, please provide the OAuth client ID and secret.') |
| 89 | + config.sh_client_id = getpass('Client ID: ') |
| 90 | + config.sh_client_secret = getpass('Client Secret: ') |
| 91 | + # config.save() ## Uncomment these lines to locally save your credentials to a configuration file |
| 92 | + # print(f'Credentials saved to {SHConfig.get_config_location()}') |
| 93 | +else: |
| 94 | + print(f'Using credentials stored here: {SHConfig.get_config_location()}') |
| 95 | +``` |
| 96 | + |
| 97 | +#### For Notebooks that use API key authentication |
| 98 | + |
| 99 | +If a notebook is using `requests` instead of the SDK, you will need to use API key authentication. |
| 100 | + |
| 101 | +``` |
| 102 | +import os |
| 103 | +import requests |
| 104 | +
|
| 105 | +# Authenticate with the Planet SDK for Python using your API key; See docs: https://docs.planet.com/develop/authentication |
| 106 | +# Check for PL_API_KEY environment variable, otherwise type in your API key. |
| 107 | +pl_api_key = os.getenv('PL_API_KEY') |
| 108 | +if not pl_api_key: |
| 109 | + import getpass |
| 110 | + pl_api_key = getpass.getpass('Planet API Key: ') |
| 111 | + os.environ['PL_API_KEY'] = pl_api_key |
| 112 | +
|
| 113 | +session = requests.Session() |
| 114 | +session.auth = (pl_api_key, '') |
| 115 | +``` |
| 116 | + |
| 117 | +And if in the same notebook, you also need to use the SDK, you can use the following: |
| 118 | + |
91 | 119 | ``` |
| 120 | +import planet |
92 | 121 |
|
93 | | -From the root directory within the docker container, run one of the following: |
94 | | - |
95 | | -1. To run all notebooks |
96 | | - ```bash |
97 | | - $> pytest tests/test_notebooks.py |
98 | | - ``` |
99 | | -1. run only notebooks in a subdirectory using |
100 | | - ```bash |
101 | | - $> pytest tests/test_notebooks.py --path <subdirectory> |
102 | | - ``` |
103 | | -1. run one or more notebooks (separated by spaces) |
104 | | - ```bash |
105 | | - $> pytest tests/test_notebooks.py --notebooks <notebook1> <notebook2> <...> |
106 | | - ``` |
107 | | -1. run only notebooks that have a given dependency, <package> |
108 | | - ```bash |
109 | | - $> pytest tests/test_notebooks.py --notebooks "$(grep -rl import <package> jupyter-notebooks/)" |
110 | | - ``` |
111 | | - |
112 | | -## Skipping Notebooks |
113 | | - |
114 | | -Some notebooks are purposefully skipped in the validation process because they |
115 | | -do not run successfully from the command line. These notebooks are specified in |
116 | | -[tests/skip_notebooks](tests/skip_notebooks). Skipping of notebooks within |
117 | | -`skip_notebooks` can be disabled by adding the `--no-skip` option to the pytest |
118 | | -command. |
| 122 | +## Authenticate with the Planet SDK for Python with your API key |
| 123 | +auth = planet.Auth.from_key(key=pl_api_key) |
| 124 | +pl_session = planet.Session(auth) |
| 125 | +pl = planet.Planet(session) |
| 126 | +``` |
0 commit comments