*********** Setup Views *********** **Last Updated:** May 2022 In this section, you will setup most of the views needed for this app. For a refresher on setting up url maps, controllers, and templates, see: :ref:`key_concepts_tutorial`. 1. Add Job Functions ==================== Create :file:`job_functions.py` and set its contents to the following: .. code-block:: python import time import dask def inc(x): time.sleep(3) return x + 1 def double(x): time.sleep(3) return x + 2 def add(x, y): time.sleep(10) return x + y def sum_up(x): time.sleep(5) return sum(x) def convert_to_dollar_sign(result): return '$' + str(result) .. important:: The :file:`job_functions.py` module contains all the functions that we will call using Dask. It is recommended that you follow the same pattern in your apps--splitting Dask functions into a separate file. Dask produces strange results when functions are defined in the same files as controllers or models. .. note:: The ``sleep`` calls in each function are to simulate functions that do real work and hence take time to run. 2. Setup Controllers ==================== Replacte the contents of :file:`controller.py` with the following: .. code-block:: python import random from django.shortcuts import render, reverse, redirect from tethys_sdk.routing import controller from django.http.response import HttpResponseRedirect from django.contrib import messages from tethys_sdk.gizmos import Button from tethys_sdk.gizmos import JobsTable from tethys_compute.models.dask.dask_job_exception import DaskJobException from tethysapp.dask_tutorial.app import DaskTutorial as app # get job manager for the app job_manager = app.get_job_manager() @controller def home(request): """ Controller for the app home page. """ jobs_button = Button( display_text='Show All Jobs', name='dask_button', attributes={ 'data-bs-toggle': 'tooltip', 'data-bs-placement': 'top', 'title': 'Show All Jobs' }, href=reverse('dask_tutorial:jobs_table') ) context = { 'jobs_button': jobs_button } return render(request, 'dask_tutorial/home.html', context) @controller def jobs_table(request): # Use job manager to get all the jobs. jobs = job_manager.list_jobs(order_by='-id', filters=None) # Table View jobs_table_options = JobsTable( jobs=jobs, column_fields=('id', 'name', 'description', 'creation_time'), hover=True, striped=False, bordered=False, condensed=False, results_url='dask_tutorial:result', refresh_interval=1000, delete_btn=True, show_detailed_status=True, ) home_button = Button( display_text='Home', name='home_button', attributes={ 'data-bs-toggle': 'tooltip', 'data-bs-placement': 'top', 'title': 'Home' }, href=reverse('dask_tutorial:home') ) context = {'jobs_table': jobs_table_options, 'home_button': home_button} return render(request, 'dask_tutorial/jobs_table.html', context) @controller def result(request, job_id): # Use job manager to get the given job. job = job_manager.get_job(job_id=job_id) # Get result and name job_result = job.result name = job.name home_button = Button( display_text='Home', name='home_button', attributes={ 'data-bs-toggle': 'tooltip', 'data-bs-placement': 'top', 'title': 'Home' }, href=reverse('dask_tutorial:home') ) jobs_button = Button( display_text='Show All Jobs', name='dask_button', attributes={ 'data-bs-toggle': 'tooltip', 'data-bs-placement': 'top', 'title': 'Show All Jobs' }, href=reverse('dask_tutorial:jobs_table') ) context = { 'result': job_result, 'name': name, 'home_button': home_button, 'jobs_button': jobs_button } return render(request, 'dask_tutorial/results.html', context) @controller def error_message(request): messages.add_message(request, messages.ERROR, 'Invalid Scheduler!') return redirect(reverse('dask_tutorial:home')) 3. Set up HTML ============== Remove the navigation menu from the app by using the ``app_no_nav.html`` base template. Replace the contents of :file:`templates/dask_tutorial/base.html` as follows: .. code-block:: html+django {% extends "tethys_apps/app_no_nav.html" %} {% load static %} {% block title %}{{ tethys_app.name }}{% endblock %} {% block app_icon %} {# The path you provided in your app.py is accessible through the tethys_app.icon context variable #} {% endblock %} {# The name you provided in your app.py is accessible through the tethys_app.name context variable #} {% block app_title %}{{ tethys_app.name }}{% endblock %} {% block app_content %} {% endblock %} {% block app_actions %} {% endblock %} {% block content_dependent_styles %} {{ block.super }} {% endblock %} {% block scripts %} {{ block.super }} {% endblock %} Replace the contents of :file:`templates/dask_tutorial/home.html` with the following: .. code-block:: html+django {% extends "dask_tutorial/base.html" %} {% load tethys_gizmos %} {% block app_actions %} {% gizmo jobs_button %} {% endblock %} Create :file:`templates/dask_tutorial/jobs_table.html` with the following contents: .. code-block:: html+django {% extends "dask_tutorial/base.html" %} {% load static tethys_gizmos %} {% load tethys_gizmos %} {% block global_scripts %} {{ block.super }} {% gizmo_dependencies global_js %} {% endblock %} {% block styles %} {{ block.super }} {% gizmo_dependencies global_css %} {% endblock %} {% block app_content %}