From 81580d5766e7b095a23486bb7025d41b8afc0d9b Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Tue, 22 Aug 2023 17:34:52 +0530 Subject: [PATCH] apply black --- .github/workflows/build.yml | 30 +++++++ .github/workflows/python-package.yml | 31 ------- environment-dev.yml | 1 + jhub_apps/__about__.py | 2 +- jhub_apps/examples/bokeh_basic.py | 10 ++- jhub_apps/examples/panel_basic.py | 1 + jhub_apps/examples/plotlydash_app.py | 25 +++--- jhub_apps/examples/streamlit_app.py | 12 +-- jhub_apps/launcher/hub_client.py | 11 +-- jhub_apps/launcher/main.py | 4 +- jhub_apps/launcher/panel_app.py | 34 ++++---- jhub_apps/main.py | 1 + jhub_apps/service/app/service.py | 8 +- jhub_apps/spawner/command.py | 118 +++++++++++++++++---------- jhub_apps/spawner/spawner.py | 13 ++- jupyterhub_config.py | 8 +- 16 files changed, 176 insertions(+), 133 deletions(-) create mode 100644 .github/workflows/build.yml delete mode 100644 .github/workflows/python-package.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 00000000..aaa62117 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,30 @@ +name: build + +on: + push: +jobs: + build: + runs-on: ubuntu-latest + defaults: + run: + shell: bash -el {0} + strategy: + fail-fast: false + matrix: + python-version: ["3.11"] + steps: + - uses: actions/checkout@v3 + + - name: Setup Miniconda + uses: conda-incubator/setup-miniconda@v2.2.0 + with: + python-version: ${{ matrix.python-version }} + environment-file: environment-dev.yml + + - name: Install Test dependencies + run: | + pip install flake8 pytest + + - name: Lint with black + uses: psf/black@stable + diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml deleted file mode 100644 index 47e21612..00000000 --- a/.github/workflows/python-package.yml +++ /dev/null @@ -1,31 +0,0 @@ -# This workflow will install Python dependencies, run tests and lint with a variety of Python versions -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python - -name: build - -on: - push: -jobs: - build: - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - python-version: ["3.11"] - steps: - - uses: actions/checkout@v3 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v3 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - python -m pip install flake8 pytest - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - - name: Lint with flake8 - run: | - # stop the build if there are Python syntax errors or undefined names - flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics diff --git a/environment-dev.yml b/environment-dev.yml index 3964d68f..6899e921 100644 --- a/environment-dev.yml +++ b/environment-dev.yml @@ -12,3 +12,4 @@ dependencies: - plotlydash-tornado-cmd - bokeh-root-cmd - jhsingle-native-proxy + - black diff --git a/jhub_apps/__about__.py b/jhub_apps/__about__.py index 11d27f8c..a4e2017f 100644 --- a/jhub_apps/__about__.py +++ b/jhub_apps/__about__.py @@ -1 +1 @@ -__version__ = '0.1' +__version__ = "0.1" diff --git a/jhub_apps/examples/bokeh_basic.py b/jhub_apps/examples/bokeh_basic.py index 3f4d17f2..5710b362 100644 --- a/jhub_apps/examples/bokeh_basic.py +++ b/jhub_apps/examples/bokeh_basic.py @@ -10,11 +10,13 @@ def modify_doc(doc): doc: A bokeh document to which elements can be added. """ x_values = list(range(10)) - y_values = [x ** 2 for x in x_values] + y_values = [x**2 for x in x_values] data_source = ColumnDataSource(data=dict(x=x_values, y=y_values)) - plot = figure(title="f(x) = x^2", - tools="crosshair,pan,reset,save,wheel_zoom", ) - plot.line('x', 'y', source=data_source, line_width=3, line_alpha=0.6) + plot = figure( + title="f(x) = x^2", + tools="crosshair,pan,reset,save,wheel_zoom", + ) + plot.line("x", "y", source=data_source, line_width=3, line_alpha=0.6) doc.add_root(plot) doc.title = "Hello World" diff --git a/jhub_apps/examples/panel_basic.py b/jhub_apps/examples/panel_basic.py index 2ce7efaa..47a1173c 100644 --- a/jhub_apps/examples/panel_basic.py +++ b/jhub_apps/examples/panel_basic.py @@ -1,5 +1,6 @@ """We can use this to test the bokeh_root_cmd""" import panel as pn + pn.extension(sizing_mode="stretch_width") diff --git a/jhub_apps/examples/plotlydash_app.py b/jhub_apps/examples/plotlydash_app.py index 66b0cc14..d4fc0b62 100644 --- a/jhub_apps/examples/plotlydash_app.py +++ b/jhub_apps/examples/plotlydash_app.py @@ -2,25 +2,26 @@ import plotly.express as px import pandas as pd -df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder_unfiltered.csv') +df = pd.read_csv( + "https://raw.githubusercontent.com/plotly/datasets/master/gapminder_unfiltered.csv" +) app = Dash(__name__) -app.layout = html.Div([ - html.H1(children='Plotly Dash App', style={'textAlign': 'center'}), - dcc.Dropdown(df.country.unique(), 'Canada', id='dropdown-selection'), - dcc.Graph(id='graph-content') -]) +app.layout = html.Div( + [ + html.H1(children="Plotly Dash App", style={"textAlign": "center"}), + dcc.Dropdown(df.country.unique(), "Canada", id="dropdown-selection"), + dcc.Graph(id="graph-content"), + ] +) -@callback( - Output('graph-content', 'figure'), - Input('dropdown-selection', 'value') -) +@callback(Output("graph-content", "figure"), Input("dropdown-selection", "value")) def update_graph(value): dff = df[df.country == value] - return px.line(dff, x='year', y='pop') + return px.line(dff, x="year", y="pop") -if __name__ == '__main__': +if __name__ == "__main__": app.run(debug=True) diff --git a/jhub_apps/examples/streamlit_app.py b/jhub_apps/examples/streamlit_app.py index ac305b93..1f925a2c 100644 --- a/jhub_apps/examples/streamlit_app.py +++ b/jhub_apps/examples/streamlit_app.py @@ -16,11 +16,11 @@ """ -with st.echo(code_location='below'): +with st.echo(code_location="below"): total_points = st.slider("Number of points in spiral", 1, 5000, 2000) num_turns = st.slider("Number of turns in spiral", 1, 100, 9) - Point = namedtuple('Point', 'x y') + Point = namedtuple("Point", "x y") data = [] points_per_turn = total_points / num_turns @@ -33,6 +33,8 @@ y = radius * math.sin(angle) data.append(Point(x, y)) - st.altair_chart(alt.Chart(pd.DataFrame(data), height=500, width=500) - .mark_circle(color='#0068c9', opacity=0.5) - .encode(x='x:Q', y='y:Q')) + st.altair_chart( + alt.Chart(pd.DataFrame(data), height=500, width=500) + .mark_circle(color="#0068c9", opacity=0.5) + .encode(x="x:Q", y="y:Q") + ) diff --git a/jhub_apps/launcher/hub_client.py b/jhub_apps/launcher/hub_client.py index 24115ec4..f62875bf 100644 --- a/jhub_apps/launcher/hub_client.py +++ b/jhub_apps/launcher/hub_client.py @@ -2,7 +2,7 @@ import requests -API_URL = 'http://127.0.0.1:8000/hub/api' +API_URL = "http://127.0.0.1:8000/hub/api" JHUB_APP_TOKEN = os.environ.get("JHUB_APP_LAUNCHER_TOKEN", "super-secret") @@ -12,10 +12,10 @@ def __init__(self, token=None): self.token = token or JHUB_APP_TOKEN def _headers(self): - return {'Authorization': f'token {self.token}'} + return {"Authorization": f"token {self.token}"} def get_users(self): - r = requests.get(API_URL + '/users', headers=self._headers()) + r = requests.get(API_URL + "/users", headers=self._headers()) r.raise_for_status() users = r.json() return users @@ -23,10 +23,7 @@ def get_users(self): def create_server(self, username, servername="foobarlar", params=None): url = f"/users/{username}/servers/{servername}" params = params or {} - data = { - "jhub_app": True, - **params - } + data = {"jhub_app": True, **params} r = requests.post(API_URL + url, headers=self._headers(), json=data) r.raise_for_status() return r.status_code diff --git a/jhub_apps/launcher/main.py b/jhub_apps/launcher/main.py index 9b21ff0e..649d29d8 100644 --- a/jhub_apps/launcher/main.py +++ b/jhub_apps/launcher/main.py @@ -5,7 +5,7 @@ def app(): pn.serve( - {'/app': create_app}, + {"/app": create_app}, port=5000, address="localhost", allow_websocket_origin=[ @@ -16,5 +16,5 @@ def app(): ) -if __name__ == '__main__': +if __name__ == "__main__": app() diff --git a/jhub_apps/launcher/panel_app.py b/jhub_apps/launcher/panel_app.py index 082d6719..22bbb23b 100644 --- a/jhub_apps/launcher/panel_app.py +++ b/jhub_apps/launcher/panel_app.py @@ -6,10 +6,10 @@ from jhub_apps.launcher.hub_client import HubClient FRAMEWORKS = { - 'Panel': 'panel', + "Panel": "panel", "Bokeh": "bokeh", - 'Streamlit': 'streamlit', - 'Voila': 'voila', + "Streamlit": "streamlit", + "Voila": "voila", "Plotly": "plotlydash", "Gradio": "gradio", } @@ -27,12 +27,14 @@ class InputFormWidget: def create_input_form(): input_form_widget = InputFormWidget( - name_input=pn.widgets.TextInput(name='Name'), - filepath_input=pn.widgets.TextInput(name='Filepath'), - description_input=pn.widgets.TextAreaInput(name='Description'), - spinner=pn.indicators.LoadingSpinner(size=30, value=True, color="secondary", bgcolor='dark', visible=True), - button_widget=pn.widgets.Button(name='Create Dashboard', button_type='primary'), - framework=pn.widgets.Select(name='Framework', options=FRAMEWORKS) + name_input=pn.widgets.TextInput(name="Name"), + filepath_input=pn.widgets.TextInput(name="Filepath"), + description_input=pn.widgets.TextAreaInput(name="Description"), + spinner=pn.indicators.LoadingSpinner( + size=30, value=True, color="secondary", bgcolor="dark", visible=True + ), + button_widget=pn.widgets.Button(name="Create Dashboard", button_type="primary"), + framework=pn.widgets.Select(name="Framework", options=FRAMEWORKS), ) input_form = pn.Column( input_form_widget.name_input, @@ -52,7 +54,9 @@ def create_dashboard(event, input_form_widget, input_form): filepath = input_form_widget.filepath_input.value description = input_form_widget.description_input.value framework = input_form_widget.framework.value - print(f"Name: {name}, Filepath: {filepath}, Description: {description}, framework: {framework}") + print( + f"Name: {name}, Filepath: {filepath}, Description: {description}, framework: {framework}" + ) hclient = HubClient() params = { "name": input_form_widget.name_input.value, @@ -65,17 +69,19 @@ def create_dashboard(event, input_form_widget, input_form): input_form.pop(-1) # TODO: Fix Url hardcoding dashboard_link = f"http://localhost:8000/user/aktech/{name}" - text_with_link = pn.pane.Markdown(f""" + text_with_link = pn.pane.Markdown( + f""" ## 🚀 Dashboard created: [{dashboard_link}]({dashboard_link}). - """) + """ + ) input_form.append(text_with_link) print(event) def create_app(): - print("*"*100) + print("*" * 100) print("CREATING APP") - print("*"*100) + print("*" * 100) input_form_widget, input_form = create_input_form() def button_callback(event): diff --git a/jhub_apps/main.py b/jhub_apps/main.py index d6654f39..4f61462d 100644 --- a/jhub_apps/main.py +++ b/jhub_apps/main.py @@ -1,5 +1,6 @@ def app(): print("Hello world") import time + time.sleep(500) return 1 diff --git a/jhub_apps/service/app/service.py b/jhub_apps/service/app/service.py index 8bda1e95..4014ff18 100644 --- a/jhub_apps/service/app/service.py +++ b/jhub_apps/service/app/service.py @@ -43,8 +43,10 @@ async def get_token(code: str = Form(...)): @router.get("/") async def index(request: Request): "Non-authenticated function that returns {'Hello': 'World'}" - script = server_document('http://127.0.0.1:5000/app') - return templates.TemplateResponse("launcher_base.html", {"request": request, "script": script}) + script = server_document("http://127.0.0.1:5000/app") + return templates.TemplateResponse( + "launcher_base.html", {"request": request, "script": script} + ) # response_model and responses dict translate to OpenAPI (Swagger) hints @@ -52,7 +54,7 @@ async def index(request: Request): @router.get( "/me", response_model=User, - responses={401: {'model': AuthorizationError}, 400: {'model': HubApiError}}, + responses={401: {"model": AuthorizationError}, 400: {"model": HubApiError}}, ) async def me(user: User = Depends(get_current_user)): "Authenticated function that returns the User model" diff --git a/jhub_apps/spawner/command.py b/jhub_apps/spawner/command.py index c4c49288..16daee3d 100644 --- a/jhub_apps/spawner/command.py +++ b/jhub_apps/spawner/command.py @@ -1,5 +1,5 @@ # TODO: Fix this hardcoding -DEFAULT_CMD = ['python', '-m', 'jhsingle_native_proxy.main', '--authtype=none'] +DEFAULT_CMD = ["python", "-m", "jhsingle_native_proxy.main", "--authtype=none"] # TODO: Fix this hardcoding EXAMPLES_PATH = { @@ -14,52 +14,84 @@ COMMANDS = { - 'voila': { - 'args': ['--destport=0', 'python', '{-}m', 'voila', f'{EXAMPLES_PATH.get("voila")}', - '{--}port={port}', - '{--}no-browser', - '{--}Voila.base_url={base_url}/', - '{--}Voila.server_url=/', - '{--}Voila.ip=0.0.0.0', - '{--}Voila.tornado_settings', 'allow_origin={origin_host}', - '--progressive', - '--ready-check-path=/voila/static/' - ], + "voila": { + "args": [ + "--destport=0", + "python", + "{-}m", + "voila", + f'{EXAMPLES_PATH.get("voila")}', + "{--}port={port}", + "{--}no-browser", + "{--}Voila.base_url={base_url}/", + "{--}Voila.server_url=/", + "{--}Voila.ip=0.0.0.0", + "{--}Voila.tornado_settings", + "allow_origin={origin_host}", + "--progressive", + "--ready-check-path=/voila/static/", + ], }, - 'streamlit': { - 'args': ['--destport=0', 'streamlit', 'run', f'{EXAMPLES_PATH.get("streamlit")}', - '{--}server.port={port}', - '{--}server.headless=True', - '{--}browser.serverAddress='+f'{origin_host}', - '{--}browser.gatherUsageStats=false'], - 'debug_args': [], + "streamlit": { + "args": [ + "--destport=0", + "streamlit", + "run", + f'{EXAMPLES_PATH.get("streamlit")}', + "{--}server.port={port}", + "{--}server.headless=True", + "{--}browser.serverAddress=" + f"{origin_host}", + "{--}browser.gatherUsageStats=false", + ], + "debug_args": [], }, - 'plotlydash': { - 'args': [ - '--destport=0', 'python', '{-}m', 'plotlydash_tornado_cmd.main', f'{EXAMPLES_PATH.get("plotlydash")}', - '{--}port={port}' + "plotlydash": { + "args": [ + "--destport=0", + "python", + "{-}m", + "plotlydash_tornado_cmd.main", + f'{EXAMPLES_PATH.get("plotlydash")}', + "{--}port={port}", ], - 'env': {'DASH_REQUESTS_PATHNAME_PREFIX': '{base_url}/'} + "env": {"DASH_REQUESTS_PATHNAME_PREFIX": "{base_url}/"}, }, - 'bokeh': { - 'args': ['--destport=0', 'python', '{-}m', 'bokeh_root_cmd.main', f'{EXAMPLES_PATH.get("bokeh")}', - '{--}port={port}', - '{--}allow-websocket-origin='+f'{origin_host}', - '{--}prefix='+f'{base_url}', - '--ready-check-path=/ready-check'] + "bokeh": { + "args": [ + "--destport=0", + "python", + "{-}m", + "bokeh_root_cmd.main", + f'{EXAMPLES_PATH.get("bokeh")}', + "{--}port={port}", + "{--}allow-websocket-origin=" + f"{origin_host}", + "{--}prefix=" + f"{base_url}", + "--ready-check-path=/ready-check", + ] }, - 'panel': { - 'args': ['--destport=0', 'python', '{-}m', 'bokeh_root_cmd.main', f'{EXAMPLES_PATH.get("panel")}', - '{--}port={port}', - '{--}debug', - '{--}allow-websocket-origin='+f'{origin_host}', - '{--}server=panel', - '{--}prefix='+f'{base_url}', - '--ready-check-path=/ready-check'] + "panel": { + "args": [ + "--destport=0", + "python", + "{-}m", + "bokeh_root_cmd.main", + f'{EXAMPLES_PATH.get("panel")}', + "{--}port={port}", + "{--}debug", + "{--}allow-websocket-origin=" + f"{origin_host}", + "{--}server=panel", + "{--}prefix=" + f"{base_url}", + "--ready-check-path=/ready-check", + ] + }, + "rshiny": { + "args": [ + "--destport=0", + "python", + "{-}m", + "rshiny_server_cmd.main", + f'{EXAMPLES_PATH.get("rshiny")}', + "{--}port={port}", + ] }, - 'rshiny': { - 'args': ['--destport=0', 'python', '{-}m', 'rshiny_server_cmd.main', f'{EXAMPLES_PATH.get("rshiny")}', - '{--}port={port}'] - } - } diff --git a/jhub_apps/spawner/spawner.py b/jhub_apps/spawner/spawner.py index 99cf05af..7607d594 100644 --- a/jhub_apps/spawner/spawner.py +++ b/jhub_apps/spawner/spawner.py @@ -8,23 +8,22 @@ class JHubSpawner(SimpleLocalProcessSpawner): - def get_args(self): """Return arguments to pass to the notebook server""" argv = super().get_args() - if self.user_options.get('argv'): - argv.extend(self.user_options['argv']) + if self.user_options.get("argv"): + argv.extend(self.user_options["argv"]) if self.user_options.get("jhub_app"): - framework = self.user_options.get('framework') - command_args = COMMANDS.get(framework)['args'] + framework = self.user_options.get("framework") + command_args = COMMANDS.get(framework)["args"] argv.extend(command_args) return argv def get_env(self): env = super().get_env() - if self.user_options.get('env'): - env.update(self.user_options['env']) + if self.user_options.get("env"): + env.update(self.user_options["env"]) return env async def start(self): diff --git a/jupyterhub_config.py b/jupyterhub_config.py index 6a77f90e..92a5c3e2 100644 --- a/jupyterhub_config.py +++ b/jupyterhub_config.py @@ -19,7 +19,7 @@ # only listen on localhost for testing -c.JupyterHub.bind_url = 'http://127.0.0.1:8000' +c.JupyterHub.bind_url = "http://127.0.0.1:8000" c.JupyterHub.allow_named_servers = True @@ -37,7 +37,7 @@ warnings.warn(msg) public_host = "http://127.0.0.1:8000" else: - public_host = os.environ["PUBLIC_HOST"].rstrip('/') + public_host = os.environ["PUBLIC_HOST"].rstrip("/") service_name = "fastapi" oauth_redirect_uri = f"{public_host}/services/{service_name}/oauth_callback" @@ -53,9 +53,9 @@ { "name": "launcher", "url": "http://127.0.0.1:5000", - "command": ["python", "-m", "jhub_apps.launcher.main"], + "command": ["python", "-m", "jhub_apps.launcher.main"], # Remove this get, set environment properly - "api_token": os.environ.get("JHUB_APP_LAUNCHER_TOKEN", "super-secret") + "api_token": os.environ.get("JHUB_APP_LAUNCHER_TOKEN", "super-secret"), }, ]