-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
359e213
commit c86693c
Showing
1 changed file
with
1 addition
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,356 +1 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "b1d69ab6", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "267d5bbf", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"# OWID - Visualize world population growth\n", | ||
"<a href=\"https://app.naas.ai/user-redirect/naas/downloader?url=https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/OWID/OWID_world_population_growth.ipynb\" target=\"_parent\"><img src=\"https://naasai-public.s3.eu-west-3.amazonaws.com/Open_in_Naas_Lab.svg\"/></a><br><br><a href=\"https://bit.ly/3JyWIk6\">Give Feedbacks</a> | <a href=\"https://app.naas.ai/user-redirect/naas/downloader?url=https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/Naas/Naas_Start_data_product.ipynb\" target=\"_parent\">Generate Data Product</a>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "5231f49b", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Tags:** #dash #dashboard #plotly #naas #asset #analytics #dropdown #callback #bootstrap #snippet" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8b9eabab", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Author:** [Zihui Ouyang](https://www.linkedin.com/in/zihui-ouyang-539626227/)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "acb27e96-54ec-49d8-8eb8-e8d94dd9ef94", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Last update:** 2023-07-31 (Created: 2023-07-31)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ab610df3", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Description:** This notebook creates an interactive plot using Dash app infrastructure with OWID's world population growth data." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "37a304c0", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**References:**\n", | ||
"- https://github.com/owid/owid-datasets/tree/master/datasets/Annual%20world%20population%20growth%20rate%20-%20OWID\n", | ||
"- https://stackoverflow.com/questions/70886359/dash-python-making-subplots-when-multiple-parameters-are-selected\n", | ||
"- https://dash-example-index.herokuapp.com/line-charts" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "af4fbd09", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Input" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "61bb4b6a-3622-4fc8-a64b-f58c3061298e", | ||
"metadata": {}, | ||
"source": [ | ||
"### Import libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "0649176e", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"try:\n", | ||
" import dash\n", | ||
" import os\n", | ||
"except:\n", | ||
" !pip install dash --user\n", | ||
" import dash\n", | ||
"try:\n", | ||
" import dash_bootstrap_components as dbc\n", | ||
"except:\n", | ||
" !pip install dash_bootstrap_components --user\n", | ||
" import dash_bootstrap_components as dbc\n", | ||
"import pandas as pd\n", | ||
"import numpy as np\n", | ||
"from dash import Dash, html, dcc, callback, Output, Input\n", | ||
"import plotly.express as px" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "cabe4219", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Setup variables\n", | ||
"- `DASH_PORT`: specify a port number for Dash\n", | ||
"- `url`: URL to get data from Excel\n", | ||
"- `title`: App title" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "c9ffa9db", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"DASH_PORT = 8050\n", | ||
"url = \"https://raw.githubusercontent.com/owid/owid-datasets/master/datasets/Annual%20world%20population%20growth%20rate%20-%20OWID/Annual%20world%20population%20growth%20rate%20-%20OWID.csv\"\n", | ||
"title = \"World population growth\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f2d0f398", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "1f81b91e", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Initialize Dash app\n", | ||
"The `os.environ.get(\"JUPYTERHUB_USER\")` is used to access the environment variable `JUPYTERHUB_USER` already stored into your Naas Lab." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "4d1f3df3", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"app = dash.Dash(\n", | ||
" title=title,\n", | ||
" requests_pathname_prefix=f'/user/{os.environ.get(\"JUPYTERHUB_USER\")}/proxy/{DASH_PORT}/',\n", | ||
" external_stylesheets=[dbc.themes.BOOTSTRAP],\n", | ||
" meta_tags=[\n", | ||
" {\"name\": \"viewport\", \"content\": \"width=device-width, initial-scale=1.0\"}\n", | ||
" ],\n", | ||
")\n", | ||
"\n", | ||
"#app = dash.Dash(title = \"World population growth\") if you are not in Naas" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "7d73d825", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Get Data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "aad6eb74", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"contents_df = pd.read_csv(url, header = 0) \n", | ||
"new_df = contents_df[contents_df['Year'] > 1949]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "560e3784", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Create Dash App" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "c2629798", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"app = Dash(title = \"Population growth in the world\")\n", | ||
"app.layout = html.Div(\n", | ||
" [\n", | ||
" html.H4(\"Population growth from 1950 to 2015\"),\n", | ||
" dcc.RangeSlider(id='slider', min=1950, max=2015, value=[1950, 2015],\n", | ||
" marks={x: str(x) for x in [1950, 1965, 1980, 1995, 2015]}),\n", | ||
" dcc.Graph(id=\"Annual population growth rate (OWID)\", figure={}, style={'display': 'none'})\n", | ||
" ]\n", | ||
")\n", | ||
"\n", | ||
"@callback(\n", | ||
" Output(\"Annual population growth rate (OWID)\", 'figure'),\n", | ||
" Output(\"Annual population growth rate (OWID)\", 'style'),\n", | ||
" Input('slider', 'value')\n", | ||
")\n", | ||
"\n", | ||
"def update_graph(year):\n", | ||
" dff = (new_df[\"Year\"] <= year[1]) & (new_df[\"Year\"] >= year[0])\n", | ||
" \n", | ||
" figures = px.line(new_df[dff], x='Year', y=\"Annual population growth rate (OWID)\", markers = True).update_layout(\n", | ||
" plot_bgcolor='rgba(0, 0, 0, 0)', height= 600)\n", | ||
" styles = {'display': 'block'} \n", | ||
" return figures, styles\n", | ||
"if __name__ == \"__main__\":\n", | ||
" app.run_server(debug=True, port=8049, use_reloader=False)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "d43335ed", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Output" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "4f1ab927", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Generate URL and show logs" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "2cb37b0c", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"if __name__ == \"__main__\":\n", | ||
" app.run_server(proxy=f\"http://127.0.0.1:{DASH_PORT}::https://app.naas.ai\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "8c8917e7-a31c-4934-b851-41ad402b502f", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.6" | ||
}, | ||
"naas": { | ||
"notebook_id": "b536a539006dced496f5dee0a0054d535f4880f440631c3b26f6f8d5f626c7d6", | ||
"notebook_path": "OWID/OWID_Visualize_GDP_per_capita_through_the_years.ipynb" | ||
}, | ||
"papermill": { | ||
"default_parameters": {}, | ||
"environment_variables": {}, | ||
"parameters": {}, | ||
"version": "2.4.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} | ||
{"cells": [{"cell_type": "markdown", "id": "b1d69ab6", "metadata": {"papermill": {}, "tags": []}, "source": "<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"}, {"cell_type": "markdown", "id": "267d5bbf", "metadata": {"papermill": {}, "tags": []}, "source": "# OWID - Visualize world population growth\n<a href=\"https://app.naas.ai/user-redirect/naas/downloader?url=https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/OWID/OWID_world_population_growth.ipynb\" target=\"_parent\"><img src=\"https://naasai-public.s3.eu-west-3.amazonaws.com/Open_in_Naas_Lab.svg\"/></a><br><br><a href=\"https://bit.ly/3JyWIk6\">Give Feedbacks</a> | <a href=\"https://app.naas.ai/user-redirect/naas/downloader?url=https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/Naas/Naas_Start_data_product.ipynb\" target=\"_parent\">Generate Data Product</a>"}, {"cell_type": "markdown", "id": "5231f49b", "metadata": {"papermill": {}, "tags": []}, "source": "**Tags:** #dash #dashboard #plotly #naas #asset #analytics #dropdown #callback #bootstrap #snippet"}, {"cell_type": "markdown", "id": "8b9eabab", "metadata": {"papermill": {}, "tags": []}, "source": "**Author:** [Zihui Ouyang](https://www.linkedin.com/in/zihui-ouyang-539626227/)"}, {"cell_type": "markdown", "id": "acb27e96-54ec-49d8-8eb8-e8d94dd9ef94", "metadata": {"papermill": {}, "tags": []}, "source": "**Last update:** 2023-08-07 (Created: 2023-08-07)"}, {"cell_type": "markdown", "id": "ab610df3", "metadata": {"papermill": {}, "tags": []}, "source": "**Description:** This notebook creates an interactive plot using Dash app infrastructure with OWID's world population growth data."}, {"cell_type": "markdown", "id": "37a304c0", "metadata": {"papermill": {}, "tags": []}, "source": "**References:**\n- https://github.com/owid/owid-datasets/tree/master/datasets/Annual%20world%20population%20growth%20rate%20-%20OWID\n- https://stackoverflow.com/questions/70886359/dash-python-making-subplots-when-multiple-parameters-are-selected\n- https://dash-example-index.herokuapp.com/line-charts"}, {"cell_type": "markdown", "id": "af4fbd09", "metadata": {"papermill": {}, "tags": []}, "source": "## Input"}, {"cell_type": "markdown", "id": "61bb4b6a-3622-4fc8-a64b-f58c3061298e", "metadata": {"tags": [], "papermill": {}}, "source": "### Import libraries"}, {"cell_type": "code", "execution_count": null, "id": "0649176e", "metadata": {"papermill": {}, "tags": []}, "outputs": [], "source": "try:\n import dash\n import os\nexcept:\n !pip install dash --user\n import dash\ntry:\n import dash_bootstrap_components as dbc\nexcept:\n !pip install dash_bootstrap_components --user\n import dash_bootstrap_components as dbc\nimport pandas as pd\nimport numpy as np\nfrom dash import Dash, html, dcc, callback, Output, Input\nimport plotly.express as px"}, {"cell_type": "markdown", "id": "cabe4219", "metadata": {"papermill": {}, "tags": []}, "source": "### Setup variables\n- `DASH_PORT`: specify a port number for Dash\n- `url`: URL to get data from Excel\n- `title`: App title"}, {"cell_type": "code", "execution_count": null, "id": "c9ffa9db", "metadata": {"papermill": {}, "tags": []}, "outputs": [], "source": "DASH_PORT = 8050\nurl = \"https://raw.githubusercontent.com/owid/owid-datasets/master/datasets/Annual%20world%20population%20growth%20rate%20-%20OWID/Annual%20world%20population%20growth%20rate%20-%20OWID.csv\"\ntitle = \"World population growth\""}, {"cell_type": "markdown", "id": "f2d0f398", "metadata": {"papermill": {}, "tags": []}, "source": "## Model"}, {"cell_type": "markdown", "id": "1f81b91e", "metadata": {"papermill": {}, "tags": []}, "source": "### Initialize Dash app\nThe `os.environ.get(\"JUPYTERHUB_USER\")` is used to access the environment variable `JUPYTERHUB_USER` already stored into your Naas Lab."}, {"cell_type": "code", "execution_count": null, "id": "4d1f3df3", "metadata": {"papermill": {}, "tags": []}, "outputs": [], "source": "app = dash.Dash(\n title=title,\n requests_pathname_prefix=f'/user/{os.environ.get(\"JUPYTERHUB_USER\")}/proxy/{DASH_PORT}/',\n external_stylesheets=[dbc.themes.BOOTSTRAP],\n meta_tags=[\n {\"name\": \"viewport\", \"content\": \"width=device-width, initial-scale=1.0\"}\n ],\n)\n\n#app = dash.Dash(title = \"World population growth\") if you are not in Naas"}, {"cell_type": "markdown", "id": "7d73d825", "metadata": {"papermill": {}, "tags": []}, "source": "### Get Data"}, {"cell_type": "code", "execution_count": null, "id": "aad6eb74", "metadata": {"papermill": {}, "tags": []}, "outputs": [], "source": "contents_df = pd.read_csv(url, header = 0) \nnew_df = contents_df[contents_df['Year'] > 1949]"}, {"cell_type": "markdown", "id": "560e3784", "metadata": {"papermill": {}, "tags": []}, "source": "### Create Dash App"}, {"cell_type": "code", "execution_count": null, "id": "c2629798", "metadata": {"papermill": {}, "tags": []}, "outputs": [], "source": "app = Dash(title = \"Population growth in the world\")\napp.layout = html.Div(\n [\n html.H4(\"Population growth from 1950 to 2015\"),\n dcc.RangeSlider(id='slider', min=1950, max=2015, value=[1950, 2015],\n marks={x: str(x) for x in [1950, 1965, 1980, 1995, 2015]}),\n dcc.Graph(id=\"Annual population growth rate (OWID)\", figure={}, style={'display': 'none'})\n ]\n)\n\n@callback(\n Output(\"Annual population growth rate (OWID)\", 'figure'),\n Output(\"Annual population growth rate (OWID)\", 'style'),\n Input('slider', 'value')\n)\n\ndef update_graph(year):\n dff = (new_df[\"Year\"] <= year[1]) & (new_df[\"Year\"] >= year[0])\n \n figures = px.line(new_df[dff], x='Year', y=\"Annual population growth rate (OWID)\", markers = True).update_layout(\n plot_bgcolor='rgba(0, 0, 0, 0)', height= 600)\n styles = {'display': 'block'} \n return figures, styles\nif __name__ == \"__main__\":\n app.run_server(debug=True, port=8049, use_reloader=False)"}, {"cell_type": "markdown", "id": "d43335ed", "metadata": {"papermill": {}, "tags": []}, "source": "## Output"}, {"cell_type": "markdown", "id": "4f1ab927", "metadata": {"papermill": {}, "tags": []}, "source": "### Generate URL and show logs"}, {"cell_type": "code", "execution_count": null, "id": "2cb37b0c", "metadata": {"papermill": {}, "tags": []}, "outputs": [], "source": "if __name__ == \"__main__\":\n app.run_server(proxy=f\"http://127.0.0.1:{DASH_PORT}::https://app.naas.ai\")"}, {"cell_type": "code", "execution_count": null, "id": "8c8917e7-a31c-4934-b851-41ad402b502f", "metadata": {"papermill": {}, "tags": []}, "outputs": [], "source": ""}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.6"}, "naas": {"notebook_id": "b536a539006dced496f5dee0a0054d535f4880f440631c3b26f6f8d5f626c7d6", "notebook_path": "OWID/OWID_Visualize_GDP_per_capita_through_the_years.ipynb"}, "papermill": {"default_parameters": {}, "environment_variables": {}, "parameters": {}, "version": "2.4.0"}}, "nbformat": 4, "nbformat_minor": 5} |