diff --git a/.github/workflows/unit.yaml b/.github/workflows/unit.yaml new file mode 100644 index 0000000..680e8c7 --- /dev/null +++ b/.github/workflows/unit.yaml @@ -0,0 +1,28 @@ +name: Build +# To test with act: gh act --secret-file $ENV_FILE --workflows .github/workflows/unit.yml +on: + push: + branches: + - main + pull_request: + +env: + EARTHENGINE_TOKEN: ${{ secrets.EARTHENGINE_TOKEN }} + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Python "3.10" + uses: actions/setup-python@v4 + with: + python-version: "3.10" + - name: Install sepal_ui + run: python -m pip install "git+https://github.com/12rambau/sepal_ui.git@sepal_pre_release" + - name: Build with sepal_ui venv + run: module_venv + - name: Install nox + run: python -m pip install nox + - name: set entry point and run tests + run: nox -s test_ui diff --git a/component/scripts/bfast_preanalysis.py b/component/scripts/bfast_preanalysis.py index 3f8efb8..dc5d3b7 100644 --- a/component/scripts/bfast_preanalysis.py +++ b/component/scripts/bfast_preanalysis.py @@ -3,8 +3,6 @@ from .helpers import * from .cloud_masking import cloud_mask_S2 -ee.Initialize() - def analysis(aoi, start, end, sensors, t2, sr): coll = None diff --git a/component/scripts/cloud_masking.py b/component/scripts/cloud_masking.py index a78c73d..02eddc0 100644 --- a/component/scripts/cloud_masking.py +++ b/component/scripts/cloud_masking.py @@ -1,8 +1,6 @@ import ee import math -ee.Initialize() - def cloud_mask_S2(image): diff --git a/component/scripts/display.py b/component/scripts/display.py index 6351d37..39c5528 100644 --- a/component/scripts/display.py +++ b/component/scripts/display.py @@ -5,9 +5,6 @@ from component import parameter as pm -ee.Initialize() - - def display_result(ee_aoi, dataset, m, vis, measure, annual): """ Display the results on the map diff --git a/component/scripts/exports.py b/component/scripts/exports.py index b1458e8..3f52ce7 100644 --- a/component/scripts/exports.py +++ b/component/scripts/exports.py @@ -11,8 +11,6 @@ from .gdrive import * from .download import digest_tiles -ee.Initialize() - def export_to_asset(aoi_model, dataset, filename, scale, output): """ @@ -29,7 +27,7 @@ def export_to_asset(aoi_model, dataset, filename, scale, output): """ # get the root folder of the user - folder = Path(ee.data.getAssetRoots()[0]["id"]) + folder = Path(f"projects/{ee.data._cloud_api_user_project}/assets/") asset_name = folder.joinpath(filename) # check if the asset already exist @@ -82,7 +80,7 @@ def export_to_sepal(aoi_model, dataset, filename, scale, output): output.add_live_msg(cm.download.start_download) # get the root folder of the user - folder = Path(ee.data.getAssetRoots()[0]["id"]) + folder = Path(f"projects/{ee.data._cloud_api_user_project}/assets/") asset_name = folder.joinpath(filename) # load the drive_handler diff --git a/component/scripts/gdrive.py b/component/scripts/gdrive.py index ff03df5..1159ab1 100644 --- a/component/scripts/gdrive.py +++ b/component/scripts/gdrive.py @@ -15,7 +15,6 @@ class gdrive(object): def __init__(self): - self.initialize = ee.Initialize() self.credentials = ee.Credentials() self.service = discovery.build( serviceName="drive", diff --git a/component/scripts/gee.py b/component/scripts/gee.py index e5f32b8..2182722 100644 --- a/component/scripts/gee.py +++ b/component/scripts/gee.py @@ -1,8 +1,8 @@ +from pathlib import Path import time import ee -ee.Initialize() from component.message import cm @@ -66,7 +66,7 @@ def is_asset(asset_id): """ # get the asset list - folder = ee.data.getAssetRoots()[0]["id"] # maybe not the most elegant way + folder = Path(f"projects/{ee.data._cloud_api_user_project}/assets/") assets = ee.data.listAssets({"parent": folder}) asset_ids = [asset["id"] for asset in assets["assets"]] diff --git a/component/scripts/helpers.py b/component/scripts/helpers.py index 2949540..5fdb9a3 100644 --- a/component/scripts/helpers.py +++ b/component/scripts/helpers.py @@ -1,6 +1,5 @@ import ee -ee.Initialize() from .cloud_masking import cloudMaskLsatTOA, cloudMaskLsatSR diff --git a/component/tile/export.py b/component/tile/export.py index 12d7f46..a8706cc 100644 --- a/component/tile/export.py +++ b/component/tile/export.py @@ -2,8 +2,6 @@ # it will help you to have control over their fonctionalities using object oriented programming import ee -ee.Initialize() - from sepal_ui import sepalwidgets as sw import ipyvuetify as v diff --git a/noxfile.py b/noxfile.py new file mode 100644 index 0000000..08bf258 --- /dev/null +++ b/noxfile.py @@ -0,0 +1,77 @@ +"""All the process that can be run using nox. + +The nox run are build in isolated environment that will be stored in .nox. to force the venv update, remove the .nox/xxx folder. +""" + +from pathlib import Path +import nbformat +from nbconvert.preprocessors import ExecutePreprocessor +from jupyter_client.kernelspec import KernelSpecManager +import nox + + +@nox.session(reuse_venv=True) +def lint(session): + """Apply the pre-commits.""" + session.install("pre-commit") + session.run("pre-commit", "run", "--a", *session.posargs) + + +@nox.session(reuse_venv=True) +def app(session): + """Run the application.""" + + entry_point = str(Path("ui.ipynb")) + + # Duplicate the entry point file + session.run("cp", entry_point, "nox_ui.ipynb") + + # change the kernel name in the entry point + session.run("entry_point", "--test", "nox_ui.ipynb") + + session.run("jupyter", "trust", entry_point) + session.run( + "voila", "--show_tracebacks=True", "--template=sepal-ui-base", "nox_ui.ipynb" + ) + + +@nox.session() +def test_ui(session): + """Run the application.""" + + ksm = KernelSpecManager() + kernel_names = list(ksm.get_all_specs()) + print(kernel_names) + + # get the current path + root_folder = Path(__file__).parent + repo_name = root_folder.name + + # Copy the ui.ipynb to test_ui.ipynb + session.run("cp", root_folder / "ui.ipynb", root_folder / "nox_ui.ipynb") + + session.run("entry_point", "--test", root_folder / "nox_ui.ipynb") + + test_notebooks = [root_folder / "nox_ui.ipynb"] + + for notebook in test_notebooks: + with open(notebook) as ff: + nb_in = nbformat.read(ff, nbformat.NO_CONVERT) + + print("Running notebook", notebook) + + try: + ep = ExecutePreprocessor(timeout=600, kernel_name=f"test-{repo_name}") + + nb_out = ep.preprocess(nb_in) + except Exception as e: + print("########### Error running notebook", notebook) + raise e + + +@nox.session(reuse_venv=True) +def jupyter(session): + """Run the application.""" + session.install("-r", "requirements.txt") + session.run("jupyter", "trust", "no_ui.ipynb") + session.run("jupyter", "notebook", "no_ui.ipynb") diff --git a/requirements.txt b/requirements.txt index 516c308..2e1cab1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,6 @@ git+https://github.com/12rambau/sepal_ui.git@sepal_pre_release # custom libs matplotlib -rasterio \ No newline at end of file +rasterio + +# rebuild environment \ No newline at end of file diff --git a/ui.ipynb b/ui.ipynb index 0d43b0c..5fe374e 100644 --- a/ui.ipynb +++ b/ui.ipynb @@ -1,18 +1,13 @@ { "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "

Setting things up for you... Thanks for waiting!

" - ] - }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ + "from sepal_ui.scripts.utils import init_ee\n", + "init_ee()\n", "from sepal_ui import sepalwidgets as sw\n", "from component.message import cm\n", "from sepal_ui import aoi\n", @@ -159,6 +154,7 @@ "pygments_lexer": "ipython3", "version": "3.10.12" }, + "title": "Coverage analysis", "voila": { "theme": "dark" }