Search code examples
dbt

DBT:How to get macros executed in overview.md which is used to overide the dbt docs main page


Requirement: To get datetimestamp in the main page of the dbt docs site , so that we know when it was last refreshed

Code:

Have created a macro and the macro is been called in the overview.md

macro

{% macro get_date_test() %}
    {{ return((date_trunc('day', current_timestamp()) | string).replace('-', '')) }}
{% endmacro %}

under the dbt folder docs>overview.md

{% docs __overview__ %}

This documentation was created-on:: {{ get_date_test }}

{% enddocs %}

dbt_project.yml

docs-paths: ["docs"]

dbt docs doesn't show the date yet

enter image description here

I tried changing to calling as below , it threw an error as dbt couldn't recognize the macro

{{ get_date_test() }}


Solution

  • dbt macros are not meant to work in markdown files. To achieve expected behaviour, run below script before dbt docs generate:

    # custom_overview.py in root of dbt project
    import time
    
    path = './models/overview.md'
    
    comment = 'This documentation was created-on:: '
    start = '{% docs __overview__ %}' 
    end = '{% enddocs %}'
    
    updated_at = time.strftime('%Y%m%d', time.localtime())
    overview_dated = start + '\n\n' + comment + updated_at + '\n\n' + end
    
    with open(path, 'w') as f:
        f.write(overview_dated)
    
    print(f'Custom overview.md saved in {path}')
    

    With a similar approach, any documentation file can be edited or overwritten.

    To sequence it nicely with generating docs, I recommend installing and using pre-commit:

    # .pre-commit-config.yaml
    repos:
      - repo: local
        hooks:
        - id: custom-overview
          entry: python ./custom_overview.py
          language: system
          types: [python]
          pass_filenames: false
          always_run: true
      - repo: https://github.com/dbt-checkpoint/dbt-checkpoint
        rev: v1.1.1
        hooks:
        - id: dbt-docs-generate
          args: ["--cmd-flags", "++threads", "64"]