<h3 dir="auto">Description</h3>
<h4 dir="auto">Background</h4>
<p dir="auto">The new Dynamic Task Mapping feature adds the attribute <code class="notranslate">map_index</code> to the <code class="notranslate">TaskInstance</code> class. When viewing information for a single mapped task instance, you must select based on this <code class="notranslate">map_index</code>, e.g. an integer value <code class="notranslate">0</code>, <code class="notranslate">1</code>, <code class="notranslate">2</code>, ... <code class="notranslate">n</code>.</p>
<p dir="auto">However, task instances can be mapped based on an iterable data type using the <code class="notranslate">.expand()</code> - using  a slightly modified <a href="https://airflow.apache.org/docs/apache-airflow/2.3.0/concepts/dynamic-task-mapping.html#simple-mapping" rel="nofollow">Airflow Simple Mapping example</a>, on a list:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="added_values = add_one.expand(x=['truck', 'car', 'bicycle'])"><pre class="notranslate"><code class="notranslate">added_values = add_one.expand(x=['truck', 'car', 'bicycle'])
</code></pre></div>
<p dir="auto">It is a challenge to view the log for the task instance <code class="notranslate">add_one</code> that used the input <code class="notranslate">'truck'</code>. In the UI, there is no indication of the element at that index, only the index. In practice, you must click through <strong>all</strong> task instances <code class="notranslate">[0, 1, 2]</code> to find the truck log.</p>
<h4 dir="auto">Proposal</h4>
<ul dir="auto">
<li>Parameter for <code class="notranslate">TaskInstance</code> that maps <code class="notranslate">map_index</code> to a value with meaning i.e. the list element at index <code class="notranslate">map_index</code></li>
<li>UI updated to show list element, if parameter is provided</li>
</ul>
<h3 dir="auto">Use case/motivation</h3>
<p dir="auto">I would like the UI to indicate the input element value for each mapped task instance, not the index <code class="notranslate">map_index</code>. This will make it easier to navigate mapped task instances.</p>
<h3 dir="auto">Related issues</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Are you willing to submit a PR?</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> Yes I am willing to submit a PR!</li>
</ul>
<h3 dir="auto">Code of Conduct</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I agree to follow this project's <a href="https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md">Code of Conduct</a></li>
</ul>
<h3 dir="auto">Description</h3>
<p dir="auto">Airflow currently exposes <code class="notranslate">map_index</code> to the user as a way of distinguishing between tasks in an expansion.  The index is unlikely to be meaningful to the user.  They probably have their own label for this action.  I'm requesting that we allow them to add that label.</p>
<p dir="auto">To see the problem, consider a dag that sends email to a list of users which is generated at runtime:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="with DAG(...) as dag:

    @dag.task
    def get_account_status():
        return [
            {
                &quot;NAME&quot;: &quot;Wintermute&quot;,
                &quot;EMAIL&quot;: &quot;wintermute@tessier-ashpool.com&quot;,
                &quot;STATUS&quot;: &quot;active&quot;,
            },
            {
                &quot;NAME&quot;: &quot;Hojo&quot;,
                &quot;EMAIL&quot;: &quot;ops@research.shinra.com&quot;,
                &quot;STATUS&quot;: &quot;delinquent&quot;,
            },
        ]

    BashOperator.partial(
        task_id=&quot;send_email&quot;,
        bash_command=dedent(
            &quot;&quot;&quot;
            cat &lt;&lt;- EOF | tee | mailx -s &quot;your account&quot; $EMAIL
            Dear $NAME,
                Your account status is $STATUS.
            EOF
            &quot;&quot;&quot;
        ),
    ).expand(env=get_account_status())"><pre class="notranslate"><span class="pl-k">with</span> <span class="pl-v">DAG</span>(...) <span class="pl-k">as</span> <span class="pl-s1">dag</span>:

    <span class="pl-en">@<span class="pl-s1">dag</span>.<span class="pl-s1">task</span></span>
    <span class="pl-k">def</span> <span class="pl-en">get_account_status</span>():
        <span class="pl-k">return</span> [
            {
                <span class="pl-s">"NAME"</span>: <span class="pl-s">"Wintermute"</span>,
                <span class="pl-s">"EMAIL"</span>: <span class="pl-s">"wintermute@tessier-ashpool.com"</span>,
                <span class="pl-s">"STATUS"</span>: <span class="pl-s">"active"</span>,
            },
            {
                <span class="pl-s">"NAME"</span>: <span class="pl-s">"Hojo"</span>,
                <span class="pl-s">"EMAIL"</span>: <span class="pl-s">"ops@research.shinra.com"</span>,
                <span class="pl-s">"STATUS"</span>: <span class="pl-s">"delinquent"</span>,
            },
        ]

    <span class="pl-v">BashOperator</span>.<span class="pl-en">partial</span>(
        <span class="pl-s1">task_id</span><span class="pl-c1">=</span><span class="pl-s">"send_email"</span>,
        <span class="pl-s1">bash_command</span><span class="pl-c1">=</span><span class="pl-en">dedent</span>(
            <span class="pl-s">"""</span>
<span class="pl-s">            cat &lt;&lt;- EOF | tee | mailx -s "your account" $EMAIL</span>
<span class="pl-s">            Dear $NAME,</span>
<span class="pl-s">                Your account status is $STATUS.</span>
<span class="pl-s">            EOF</span>
<span class="pl-s">            """</span>
        ),
    ).<span class="pl-en">expand</span>(<span class="pl-s1">env</span><span class="pl-c1">=</span><span class="pl-en">get_account_status</span>())</pre></div>
<p dir="auto">Notice that in the grid view, it's not obvious which task goes with which user:</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/5834582/163418431-3180a29d-b9c0-4bbc-9a80-05ad5e4f34e7.png"><img width="1106" alt="Screen Shot 2022-04-14 at 8 56 09 AM" src="https://user-images.githubusercontent.com/5834582/163418431-3180a29d-b9c0-4bbc-9a80-05ad5e4f34e7.png" style="max-width: 100%;"></a></p>
<h3 dir="auto">Use case/motivation</h3>
<p dir="auto">I'd like to be able to explicitly assign a name to each expanded task, that way I can later go look at the right one.  I would like this name to be used (when available) anywhere that the user interacts with the expanded task.</p>
<p dir="auto">In cases where the user provides no names, perhaps we can generate some.  For instance, this expansion generates four instances.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="BashOperator.partial(task_id=&quot;greet&quot;).expand(
    bash_command=[&quot;echo hello $USER&quot;, &quot;echo goodbye $USER&quot;],
    env=[{&quot;USER&quot;: &quot;foo&quot;}, {&quot;USER&quot;: &quot;bar&quot;}],
)"><pre class="notranslate"><span class="pl-v">BashOperator</span>.<span class="pl-en">partial</span>(<span class="pl-s1">task_id</span><span class="pl-c1">=</span><span class="pl-s">"greet"</span>).<span class="pl-en">expand</span>(
    <span class="pl-s1">bash_command</span><span class="pl-c1">=</span>[<span class="pl-s">"echo hello $USER"</span>, <span class="pl-s">"echo goodbye $USER"</span>],
    <span class="pl-s1">env</span><span class="pl-c1">=</span>[{<span class="pl-s">"USER"</span>: <span class="pl-s">"foo"</span>}, {<span class="pl-s">"USER"</span>: <span class="pl-s">"bar"</span>}],
)</pre></div>
<p dir="auto">The friendliest way would be to use the requested feature name each task:</p>
<ul dir="auto">
<li><code class="notranslate">hi_foo</code></li>
<li><code class="notranslate">hi_bar</code></li>
<li><code class="notranslate">bye_foo</code></li>
<li><code class="notranslate">bye_bar</code></li>
</ul>
<p dir="auto">As it is, the user will see:</p>
<ul dir="auto">
<li><code class="notranslate">1</code></li>
<li><code class="notranslate">2</code></li>
<li><code class="notranslate">3</code></li>
<li><code class="notranslate">4</code></li>
</ul>
<p dir="auto">But if the user doesn't give names, maybe we should generate some names for them:</p>
<ul dir="auto">
<li><code class="notranslate">bash_command_1_env_1</code></li>
<li><code class="notranslate">bash_command_1_env_2</code></li>
<li><code class="notranslate">bash_command_2_env_1</code></li>
<li><code class="notranslate">bash_command_2_env_2</code></li>
</ul>
<p dir="auto">I don't know.  I'm creating this issue so we have a place to discuss it.</p>
<h3 dir="auto">Related issues</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Are you willing to submit a PR?</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> Yes I am willing to submit a PR!</li>
</ul>
<h3 dir="auto">Code of Conduct</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I agree to follow this project's <a href="https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md">Code of Conduct</a></li>
</ul>