tabbed_dashboard.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """Loads datasets, dashboards and slices in a new superset instance"""
  18. import json
  19. import textwrap
  20. from superset import db
  21. from superset.models.dashboard import Dashboard
  22. from superset.models.slice import Slice
  23. from .helpers import update_slice_ids
  24. def load_tabbed_dashboard(_=False):
  25. """Creating a tabbed dashboard"""
  26. print("Creating a dashboard with nested tabs")
  27. slug = "tabbed_dash"
  28. dash = db.session.query(Dashboard).filter_by(slug=slug).first()
  29. if not dash:
  30. dash = Dashboard()
  31. # reuse charts in "World's Bank Data and create
  32. # new dashboard with nested tabs
  33. tabbed_dash_slices = set()
  34. tabbed_dash_slices.add("Region Filter")
  35. tabbed_dash_slices.add("Growth Rate")
  36. tabbed_dash_slices.add("Treemap")
  37. tabbed_dash_slices.add("Box plot")
  38. js = textwrap.dedent(
  39. """\
  40. {
  41. "CHART-c0EjR-OZ0n": {
  42. "children": [],
  43. "id": "CHART-c0EjR-OZ0n",
  44. "meta": {
  45. "chartId": 870,
  46. "height": 50,
  47. "sliceName": "Box plot",
  48. "width": 4
  49. },
  50. "parents": [
  51. "ROOT_ID",
  52. "TABS-lV0r00f4H1",
  53. "TAB-NF3dlrWGS",
  54. "ROW-7G2o5uDvfo"
  55. ],
  56. "type": "CHART"
  57. },
  58. "CHART-dxV7Il74hH": {
  59. "children": [],
  60. "id": "CHART-dxV7Il74hH",
  61. "meta": {
  62. "chartId": 797,
  63. "height": 50,
  64. "sliceName": "Treemap",
  65. "width": 4
  66. },
  67. "parents": [
  68. "ROOT_ID",
  69. "TABS-lV0r00f4H1",
  70. "TAB-gcQJxApOZS",
  71. "TABS-afnrUvdxYF",
  72. "TAB-jNNd4WWar1",
  73. "ROW-7ygtDczaQ"
  74. ],
  75. "type": "CHART"
  76. },
  77. "CHART-jJ5Yj1Ptaz": {
  78. "children": [],
  79. "id": "CHART-jJ5Yj1Ptaz",
  80. "meta": {
  81. "chartId": 789,
  82. "height": 50,
  83. "sliceName": "World's Population",
  84. "width": 4
  85. },
  86. "parents": [
  87. "ROOT_ID",
  88. "TABS-lV0r00f4H1",
  89. "TAB-NF3dlrWGS",
  90. "TABS-CSjo6VfNrj",
  91. "TAB-z81Q87PD7",
  92. "ROW-G73z9PIHn"
  93. ],
  94. "type": "CHART"
  95. },
  96. "CHART-z4gmEuCqQ5": {
  97. "children": [],
  98. "id": "CHART-z4gmEuCqQ5",
  99. "meta": {
  100. "chartId": 788,
  101. "height": 50,
  102. "sliceName": "Region Filter",
  103. "width": 4
  104. },
  105. "parents": [
  106. "ROOT_ID",
  107. "TABS-lV0r00f4H1",
  108. "TAB-NF3dlrWGS",
  109. "TABS-CSjo6VfNrj",
  110. "TAB-EcNm_wh922",
  111. "ROW-LCjsdSetJ"
  112. ],
  113. "type": "CHART"
  114. },
  115. "DASHBOARD_VERSION_KEY": "v2",
  116. "GRID_ID": {
  117. "children": [],
  118. "id": "GRID_ID",
  119. "type": "GRID"
  120. },
  121. "HEADER_ID": {
  122. "id": "HEADER_ID",
  123. "meta": {
  124. "text": "Tabbed Dashboard"
  125. },
  126. "type": "HEADER"
  127. },
  128. "ROOT_ID": {
  129. "children": [
  130. "TABS-lV0r00f4H1"
  131. ],
  132. "id": "ROOT_ID",
  133. "type": "ROOT"
  134. },
  135. "ROW-7G2o5uDvfo": {
  136. "children": [
  137. "CHART-c0EjR-OZ0n"
  138. ],
  139. "id": "ROW-7G2o5uDvfo",
  140. "meta": {
  141. "background": "BACKGROUND_TRANSPARENT"
  142. },
  143. "parents": [
  144. "ROOT_ID",
  145. "TABS-lV0r00f4H1",
  146. "TAB-NF3dlrWGS"
  147. ],
  148. "type": "ROW"
  149. },
  150. "ROW-7ygtDczaQ": {
  151. "children": [
  152. "CHART-dxV7Il74hH"
  153. ],
  154. "id": "ROW-7ygtDczaQ",
  155. "meta": {
  156. "background": "BACKGROUND_TRANSPARENT"
  157. },
  158. "parents": [
  159. "ROOT_ID",
  160. "TABS-lV0r00f4H1",
  161. "TAB-gcQJxApOZS",
  162. "TABS-afnrUvdxYF",
  163. "TAB-jNNd4WWar1"
  164. ],
  165. "type": "ROW"
  166. },
  167. "ROW-G73z9PIHn": {
  168. "children": [
  169. "CHART-jJ5Yj1Ptaz"
  170. ],
  171. "id": "ROW-G73z9PIHn",
  172. "meta": {
  173. "background": "BACKGROUND_TRANSPARENT"
  174. },
  175. "parents": [
  176. "ROOT_ID",
  177. "TABS-lV0r00f4H1",
  178. "TAB-NF3dlrWGS",
  179. "TABS-CSjo6VfNrj",
  180. "TAB-z81Q87PD7"
  181. ],
  182. "type": "ROW"
  183. },
  184. "ROW-LCjsdSetJ": {
  185. "children": [
  186. "CHART-z4gmEuCqQ5"
  187. ],
  188. "id": "ROW-LCjsdSetJ",
  189. "meta": {
  190. "background": "BACKGROUND_TRANSPARENT"
  191. },
  192. "parents": [
  193. "ROOT_ID",
  194. "TABS-lV0r00f4H1",
  195. "TAB-NF3dlrWGS",
  196. "TABS-CSjo6VfNrj",
  197. "TAB-EcNm_wh922"
  198. ],
  199. "type": "ROW"
  200. },
  201. "TAB-EcNm_wh922": {
  202. "children": [
  203. "ROW-LCjsdSetJ"
  204. ],
  205. "id": "TAB-EcNm_wh922",
  206. "meta": {
  207. "text": "row tab 1"
  208. },
  209. "parents": [
  210. "ROOT_ID",
  211. "TABS-lV0r00f4H1",
  212. "TAB-NF3dlrWGS",
  213. "TABS-CSjo6VfNrj"
  214. ],
  215. "type": "TAB"
  216. },
  217. "TAB-NF3dlrWGS": {
  218. "children": [
  219. "ROW-7G2o5uDvfo",
  220. "TABS-CSjo6VfNrj"
  221. ],
  222. "id": "TAB-NF3dlrWGS",
  223. "meta": {
  224. "text": "Tab A"
  225. },
  226. "parents": [
  227. "ROOT_ID",
  228. "TABS-lV0r00f4H1"
  229. ],
  230. "type": "TAB"
  231. },
  232. "TAB-gcQJxApOZS": {
  233. "children": [
  234. "TABS-afnrUvdxYF"
  235. ],
  236. "id": "TAB-gcQJxApOZS",
  237. "meta": {
  238. "text": "Tab B"
  239. },
  240. "parents": [
  241. "ROOT_ID",
  242. "TABS-lV0r00f4H1"
  243. ],
  244. "type": "TAB"
  245. },
  246. "TAB-jNNd4WWar1": {
  247. "children": [
  248. "ROW-7ygtDczaQ"
  249. ],
  250. "id": "TAB-jNNd4WWar1",
  251. "meta": {
  252. "text": "New Tab"
  253. },
  254. "parents": [
  255. "ROOT_ID",
  256. "TABS-lV0r00f4H1",
  257. "TAB-gcQJxApOZS",
  258. "TABS-afnrUvdxYF"
  259. ],
  260. "type": "TAB"
  261. },
  262. "TAB-z81Q87PD7": {
  263. "children": [
  264. "ROW-G73z9PIHn"
  265. ],
  266. "id": "TAB-z81Q87PD7",
  267. "meta": {
  268. "text": "row tab 2"
  269. },
  270. "parents": [
  271. "ROOT_ID",
  272. "TABS-lV0r00f4H1",
  273. "TAB-NF3dlrWGS",
  274. "TABS-CSjo6VfNrj"
  275. ],
  276. "type": "TAB"
  277. },
  278. "TABS-CSjo6VfNrj": {
  279. "children": [
  280. "TAB-EcNm_wh922",
  281. "TAB-z81Q87PD7"
  282. ],
  283. "id": "TABS-CSjo6VfNrj",
  284. "meta": {},
  285. "parents": [
  286. "ROOT_ID",
  287. "TABS-lV0r00f4H1",
  288. "TAB-NF3dlrWGS"
  289. ],
  290. "type": "TABS"
  291. },
  292. "TABS-afnrUvdxYF": {
  293. "children": [
  294. "TAB-jNNd4WWar1"
  295. ],
  296. "id": "TABS-afnrUvdxYF",
  297. "meta": {},
  298. "parents": [
  299. "ROOT_ID",
  300. "TABS-lV0r00f4H1",
  301. "TAB-gcQJxApOZS"
  302. ],
  303. "type": "TABS"
  304. },
  305. "TABS-lV0r00f4H1": {
  306. "children": [
  307. "TAB-NF3dlrWGS",
  308. "TAB-gcQJxApOZS"
  309. ],
  310. "id": "TABS-lV0r00f4H1",
  311. "meta": {},
  312. "parents": [
  313. "ROOT_ID"
  314. ],
  315. "type": "TABS"
  316. }
  317. }
  318. """
  319. )
  320. pos = json.loads(js)
  321. slices = [
  322. db.session.query(Slice).filter_by(slice_name=name).first()
  323. for name in tabbed_dash_slices
  324. ]
  325. slices = sorted(slices, key=lambda x: x.id)
  326. update_slice_ids(pos, slices)
  327. dash.position_json = json.dumps(pos, indent=4)
  328. dash.slices = slices
  329. dash.dashboard_title = "Tabbed Dashboard"
  330. dash.slug = slug
  331. db.session.merge(dash)
  332. db.session.commit()