Repository: apache/airflow

Issue:
show_dag (save) feature is not supporting any other orientation except 'LR'
<!--

Welcome to Apache Airflow!  For a smooth issue process, try to answer the following questions.
Don't worry if they're not all applicable; just try to include what you can :-)

If you need to include code snippets or logs, please put them in fenced code
blocks.  If they're super-long, please use the details tag like
<details><summary>super-long log</summary> lots of stuff </details>

Please delete these comment blocks before submitting the issue.

-->

<!--

IMPORTANT!!!

PLEASE CHECK "SIMILAR TO X EXISTING ISSUES" OPTION IF VISIBLE
NEXT TO "SUBMIT NEW ISSUE" BUTTON!!!

PLEASE CHECK IF THIS ISSUE HAS BEEN REPORTED PREVIOUSLY USING SEARCH!!!

Please complete the next sections or the issue will be closed.
This questions are the first thing we need to know to understand the context.

-->

**Apache Airflow version**: master

**What happened**: I have DAG in TB orientation and when I tried to save that DAG graph to png file, it is not honoring the orientation, it is taking in 'LR' orientation only.

<!-- (please include exact error messages if you can) -->

**What you expected to happen**: The graph in the png file should be similar to the orientation of the DAG.

<!-- What do you think went wrong? -->

**How to reproduce it**: Change the orientation of the DAG other than 'LR' and try to save the graph.
<!---

As minimally and precisely as possible. Keep in mind we do not have access to your cluster or dags.

If you are using kubernetes, please attempt to recreate the issue using minikube or kind.

## Install minikube/kind

- Minikube https://minikube.sigs.k8s.io/docs/start/
- Kind https://kind.sigs.k8s.io/docs/user/quick-start/

If this is a UI bug, please provide a screenshot of the bug or a link to a youtube video of the bug in action

You can include images using the .md sytle of
![alt text](http://url/to/img.png)

To record a screencast, mac users can use QuickTime and then create an unlisted youtube video with the resulting .mov file.

--->




Discussion:
Thanks for opening your first issue here! Be sure to follow the issue template!


Resolving patch:
diff --git a/airflow/utils/dot_renderer.py b/airflow/utils/dot_renderer.py
--- a/airflow/utils/dot_renderer.py
+++ b/airflow/utils/dot_renderer.py
@@ -57,7 +57,8 @@ def render_dag(dag: DAG, tis: Optional[List[TaskInstance]] = None) -> graphviz.D
     :return: Graphviz object
     :rtype: graphviz.Digraph
     """
-    dot = graphviz.Digraph(dag.dag_id, graph_attr={"rankdir": "LR", "labelloc": "t", "label": dag.dag_id})
+    dot = graphviz.Digraph(dag.dag_id, graph_attr={"rankdir": dag.orientation if dag.orientation else "LR",
+                                                   "labelloc": "t", "label": dag.dag_id})
     states_by_task_id = None
     if tis is not None:
         states_by_task_id = {ti.task_id: ti.state for ti in tis}


Test patch:
diff --git a/tests/utils/test_dot_renderer.py b/tests/utils/test_dot_renderer.py
--- a/tests/utils/test_dot_renderer.py
+++ b/tests/utils/test_dot_renderer.py
@@ -76,3 +76,33 @@ def test_should_render_dag_with_task_instances(self):
         self.assertIn('first [color=black fillcolor=tan shape=rectangle style="filled,rounded"]', source)
         self.assertIn('second [color=white fillcolor=green shape=rectangle style="filled,rounded"]', source)
         self.assertIn('third [color=black fillcolor=lime shape=rectangle style="filled,rounded"]', source)
+
+    def test_should_render_dag_orientation(self):
+        orientation = "TB"
+        dag = DAG(dag_id="DAG_ID", orientation=orientation)
+        task_1 = BashOperator(dag=dag, start_date=START_DATE, task_id="first", bash_command="echo 1")
+        task_2 = BashOperator(dag=dag, start_date=START_DATE, task_id="second", bash_command="echo 1")
+        task_3 = PythonOperator(
+            dag=dag, start_date=START_DATE, task_id="third", python_callable=mock.MagicMock()
+        )
+        task_1 >> task_2
+        task_1 >> task_3
+        tis = [
+            TaskInstance(task_1, execution_date=START_DATE, state=State.SCHEDULED),
+            TaskInstance(task_2, execution_date=START_DATE, state=State.SUCCESS),
+            TaskInstance(task_3, execution_date=START_DATE, state=State.RUNNING),
+        ]
+        dot = dot_renderer.render_dag(dag, tis=tis)
+        source = dot.source
+        # Should render DAG title with orientation
+        self.assertIn("label=DAG_ID", source)
+        self.assertIn(f'label=DAG_ID labelloc=t rankdir={orientation}', source)
+
+        # Change orientation
+        orientation = "LR"
+        dag = DAG(dag_id="DAG_ID", orientation=orientation)
+        dot = dot_renderer.render_dag(dag, tis=tis)
+        source = dot.source
+        # Should render DAG title with orientation
+        self.assertIn("label=DAG_ID", source)
+        self.assertIn(f'label=DAG_ID labelloc=t rankdir={orientation}', source)