Discover this year's submissions to the Dataiku Frontrunner Awards and give kudos to your favorite use cases and success stories!READ MORE

Export Jupyter notebook in a scenario step

Sidiya
Level 2
Export Jupyter notebook in a scenario step

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

0 Kudos
3 Replies
ZachM
Dataiker
Dataiker

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

0 Kudos
Sidiya
Level 2
Author

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 !

 

0 Kudos
ZachM
Dataiker
Dataiker

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

Labels

?
Labels (1)
A banner prompting to get Dataiku