Export Jupyter notebook in a scenario step

Sidiya
Sidiya Registered Posts: 4 ✭✭✭

Hello,

Im using the step Export notebook in a scenario step and sending it as an attached html file using mail reporter. The problem is that the exported notebook will have always “notebook.html” as its name.

Any idea on how to keep the initial name of the notebook in the project or rename it with a global variable in the scenario steps. The idea is to use the date of the execution of the scenario to rename the Exported file “control_outcome_2022-06-15.html” for example.

Thanks,


Operating system used: Windows 10

Tagged:

Answers

  • Zach
    Zach Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 153 Dataiker
    edited July 17

    Hi @Sidiya
    ,

    There's no way to change the name of the attached notebook, but if your goal is to get the date of execution, there are a couple other ways that you can get it:

    1. You can include the date that the notebook was run within the notebook's output. The following example code will print the current date. You can add it somewhere in your notebook:
      from datetime import date
      
      current_day = date.today()
      print('Executed on:', current_day)​
    2. You can create a custom variable for the mail reporter that contains the date the scenario started or ended. Click the CREATE CUSTOM VARIABLES button in the mail reporter settings, and replace the preexisting code with the following code:
      import json
      from datetime import date
      
      # Compute your additional variables from the list of report items
      # and return them as a dictionary.
      def get_variables(items_json, scenario_run_json, step_run_output_json):
          scenario_run = json.loads(scenario_run_json)
      
          # POSIX timestamps (in milliseconds) indicating the times that the
          # scenario started and ended.
          time_started = scenario_run['start']
          time_ended = scenario_run['end']
      
          # Convert the timestamps to dates
          date_started = date.fromtimestamp(time_started / 1000)
          date_ended = date.fromtimestamp(time_ended / 1000)
      
          # Return a dict of custom variables
          return {
              'scenarioStarted': date_started.isoformat(),
              'scenarioEnded': date_ended.isoformat()
          }​

    This will create 2 new variables called ${scenarioStarted} and ${scenarioEnded}. You can use them in the subject or the body of the email.A5852600-A815-48F8-927F-14BC18414FE1_1_201_a.jpeg

    If you have any questions, feel free to let me know.

    Thank you,

    Zach

  • Sidiya
    Sidiya Registered Posts: 4 ✭✭✭

    Hello,

    Thank you for the answer.

    Can I at least keep the initial name of the notebook as it is the case when the notebbok is exported manually ?

    I just find it a little bit frustrating to have the expoprted file always named as 'notebook.html'.

    Thanks for your help !

  • Zach
    Zach Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 153 Dataiker
    edited July 17

    Hi @Sidiya
    ,

    You can obtain the name of the notebook similarly to method 2 that I described in my previous post. You'll want to use the following code for the custom variable:

    import json
    
    # Compute your additional variables from the list of report items 
    # and return them as a dictionary.
    def get_variables(items_json, scenario_run_json, step_run_output_json):
        scenario_run = json.loads(scenario_run_json)
    
        # Get the name of the first attached notebook in the first reporter
        # in the scenario.
        notebook_id = scenario_run['scenario']['reporters'][0]['messaging']['configuration']['attachments'][0]['params']['attachedNotebookId']
    
        return {
            'notebookID': notebook_id
        }

    This will create a variable called ${notebookID} which is the name of the attached notebook. You can use it in the subject or body of the email like I described in my previous post.

    Note that this method will only work if you have a single notebook attached. If you have multiple notebooks attached, there's no way to differentiate between them using this method.

    Also, the name of the attachment will always be notebook.html. There's no way to change that unfortunately. Sorry for the inconvenience.

    Thanks,

    Zach

  • Surbhi2512
    Surbhi2512 Dataiku DSS Core Designer, Registered Posts: 2

    Hey @ZachM
    ,

    CAn you please direct me towards any documentation where I can find the details on the arguments being passed in this get_variables function.

    I am trying to create a custom variable based on the already available variable-> $dssURL

  • Zach
    Zach Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 153 Dataiker
    edited July 17

    Hi @Surbhi2512
    ,


    CAn you please direct me towards any documentation where I can find the details on the arguments being passed in this get_variables function.

    I'm using the following custom-variable code in order to see what the arguments look like:

    # compute your additional variables from the list of report items 
    # and return them as a dictionary.
    def get_variables(items_json, scenario_run_json, step_run_output_json):
        return {
            "items": items_json,
            "scenario_run": scenario_run_json,
            "step_run_output": step_run_output_json
        }

    You can then use the following template to send the arguments via a mail reporter:

    image.png

    items: ${items}
    
    scenario_run: ${scenario_run}
    
    step_run_output: ${step_run_output}


    I am trying to create a custom variable based on the already available variable-> $dssURL


    The already-available variables aren't accessible by default, but you can make them accessible by assigning them to a scenario variable:

    1. Add a "Define scenario variables" step to your scenario. Turn "Evaluated variables" on, and create a new variable where the value is dssURL. You can also make any changes to the variable here using Formulas if you'd like. In the following screenshot, I use replace() to change the port number in the URL: image.png

    2. Set your custom-variable code to the following code. This will create the ${custom_variable} variable.
      import json
      
      # compute your additional variables from the list of report items 
      # and return them as a dictionary.
      def get_variables(items_json, scenario_run_json, step_run_output_json):
          scenario_run = json.loads(scenario_run_json)
          custom_variable = scenario_run["variables"]["custom_variable"]
          
          # Make changes to your custom variable here if needed
          
          return {
              "custom_variable": custom_variable
          }​

    Thanks,

    Zach

Setup Info
    Tags
      Help me…