Skip to content

Commit 4f139e7

Browse files
Rewrite Customize site page to account for all changes made to Custom DC website (#693)
* remove references to dataclass objects * Fix a copy-paste error. * remove extra file * Fix a copy-paste error. * Remove unused file * Start customization update * Really remove references from quickstart this time * Fix logo filename * Correct definition of provenance and add 3 items to the glossary * Rewrite of provenance and addition of items to glossary * revert customizing files * Fix messed-up push * fix glossary again * Fix formatting and preview images * Update data_model.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update data_model.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update glossary.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Tiny wording change * Restart draft of UI customization * New website images and start of customization doc rewrite * More revisions * More changes * Finish option 1 * More changes * Finish first draft * Rewrite to highlight commands and code * Overhauled formatting * Finalize first draft for review * Remove duplicate caused by merge * Apply suggestions from code review Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Restore formatting killed by Gemini Code Assist and make "myproject" consistent throughout. * Fix formatting of header bar option --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent e244b63 commit 4f139e7

2 files changed

Lines changed: 215 additions & 71 deletions

File tree

custom_dc/custom_ui.md

Lines changed: 213 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,66 +15,216 @@ This page shows you how to customize the UI of your local instance. This is step
1515

1616
## Overview
1717

18-
The default custom Data Commons image provides a bare-bones UI that you will undoubtedly want to customize to your liking. Data Commons uses the Python [Flask](https://flask.palletsprojects.com/en/3.0.x/#) web framework and [Jinja](https://jinja.palletsprojects.com/en/3.1.x/templates/) HTML templates. If you're not familiar with these, the following documents are good starting points:
19-
20-
- [Flask Templates](https://flask.palletsprojects.com/en/3.0.x/tutorial/templates/)
21-
- [Flask Static Files](https://flask.palletsprojects.com/en/3.0.x/tutorial/static/)
22-
23-
HTML and CSS customization files are provided as samples to get you started. They are as follows:
24-
25-
<table>
26-
<thead>
27-
<tr>
28-
<th>Directory/files</th>
29-
<th>Description</th>
30-
</tr>
31-
</thead>
32-
<tbody>
33-
<tr>
34-
<td width="450"><a href="https://github.com/datacommonsorg/website/blob/master/server/templates/custom_dc/custom/base.html"><code>server/templates/custom_dc/custom/base.html</code></a></td>
35-
<td>Template file for the site's header and footer. More details below.</td>
36-
</tr>
37-
<tr>
38-
<td><a href="https://github.com/datacommonsorg/website/blob/master/server/templates/custom_dc/custom/homepage.html"><code>server/templates/custom_dc/custom/homepage.html</code></a></td>
39-
<td>Template file for the site's home page</td>
40-
</tr>
41-
<tr>
42-
<td><a href="https://github.com/datacommonsorg/website/blob/master/server/templates/custom_dc/custom/browser_landing.html"><code>server/templates/custom_dc/custom/browser_landing.html</code></a></td>
43-
<td>Template file for the Graph Browser landing page. You can change the default text.</td>
44-
</tr>
45-
<tr>
46-
<td><a href="https://github.com/datacommonsorg/website/blob/master/static/custom_dc/custom/overrides.css"><code>static/custom_dc/custom/overrides.css</code></a></td>
47-
<td>Stylesheet overrides for the site. </td>
48-
</tr>
49-
<tr>
50-
<td><a href="https://github.com/datacommonsorg/website/blob/a246b809e1d756e0512ed4f09b59137a64dc6e4e/static/custom_dc/custom/logo.png"><code>static/custom_dc/custom/logo.png</code></a></td>
51-
<td>Sample logo file – replace with your own.</td>
52-
</tr>
53-
</tbody>
54-
</table>
55-
56-
Note that the `custom` parent directory is customizable as the `FLASK_ENV` environment variable. You can rename the directory as desired and update the environment variable in `custom_dc/env.list`.
57-
58-
If you have renamed the parent `custom` directory, be sure to use that name in the flag.
59-
60-
> **Note:** Currently, making changes to any of the files in the `static/` directory, even if you're testing locally, requires that you rebuild a local version of the repo to pick up the changes, as described in [Build a local image](/custom_dc/build_image.html#build-repo). We plan to fix this in the near future.
61-
62-
## Customize HTML templates {#html-templates}
63-
64-
You can customize the page header and footer (by default, empty) in [base.html](https://github.com/datacommonsorg/website/blob/master/server/templates/custom_dc/custom/base.html) by adding or changing the HTML elements within the `<header></header>` and `<footer></footer>` tags, respectively.
65-
66-
<!--TODO: Add an example of customization e.g. different colors or text-->
67-
68-
## Customize Javascript and styles {#styles}
69-
70-
Use the [overrides.css](https://github.com/datacommonsorg/website/blob/master/static/custom_dc/custom/overrides.css) file to customize the default Data Commons styles. The file provides a default color override. You can add all style overrides to that file.
71-
72-
Alternatively, if you have existing CSS and Javascript files, put them under the [/static/custom_dc/custom](https://github.com/datacommonsorg/website/blob/master/static/custom_dc/custom) folder. Then include these files in the `<head>` section of the corresponding HTML files as:
73-
74-
<pre>
75-
&lt;link href="/custom_dc/custom/<var>FILENAME</var>.css|js" rel="stylesheet" /&gt;
76-
</pre>
77-
78-
See [`server/templates/custom_dc/custom/new.html`](https://github.com/datacommonsorg/website/blob/master/server/templates/custom_dc/custom/new.html) as an example.
79-
80-
18+
The Custom Data Commons image provides a default site user interface that you will want to customize. The site uses the Python [Flask](https://flask.palletsprojects.com/en/3.0.x/#){: target="_blank"} web framework, [React](https://react.dev/){: target="_blank"} Javascript components and [Jinja](https://jinja.palletsprojects.com/en/3.1.x/templates/){: target="_blank"} HTML templates.
19+
20+
This page describes how you can reuse and modify various code and configuration files that are provided for Custom Data Commons in the `website` repo.
21+
22+
{: #setup}
23+
## Before you start: Set up your environment
24+
25+
The files that control the Custom Data Commons UI are in the following directories in the `website` repo:
26+
- [`server/app_env/`](https://github.com/datacommonsorg/website/tree/master/server/app_env){: target="_blank"}: Python web server configuration
27+
- [`server/templates/custom_dc/custom/`](https://github.com/datacommonsorg/website/tree/master/server/templates/custom_dc/custom){: target="_blank"}: Jinja HTML templates
28+
- [`server/templates/tools/`](https://github.com/datacommonsorg/website/tree/master/server/templates/tools){: target="_blank"}: JSON files for visualization tools
29+
- [`server/config/custom_dc/custom/`](https://github.com/datacommonsorg/website/tree/master/server/config/custom_dc/custom){: target="_blank"}: JSON files for layout elements
30+
- [`static/custom_dc/custom/`](https://github.com/datacommonsorg/website/tree/master/static/custom_dc/custom){: target="_blank"}: CSS and image files
31+
32+
While it's possible to edit all of the files in place, this risks causing merge conflicts or overwrites whenever you sync to the latest stable release. Instead, we recommend the following procedure.
33+
34+
### Step 1: Set up your Flask templates environment
35+
36+
1. Choose a simple name for directories that will host template and static files, e.g. `myproject`.
37+
1. Create a new subdirectory under `server/templates/custom_dc` using the new name. For example:
38+
```
39+
cd website/server/templates/custom_dc
40+
mkdir myproject
41+
```
42+
1. For any of the HTML template files you would like to edit directly, copy them from the `custom` subdirectory into your new directory.
43+
```
44+
cp custom/*.html myproject/
45+
```
46+
For additional HTML files, you can store them here too; be sure to set required variables as described in the comments at the top of [`base.html`](https://github.com/datacommonsorg/website/blob/master/server/templates/custom_dc/custom/base.html){: target="_blank"}.
47+
48+
1. If you'd like to customize the examples that appear in the visualization tools, copy any or all of the JSON files from `server/templates/tools/` into your new directory.
49+
```
50+
cp ../tools/*.json myproject/
51+
```
52+
1. If you'd like to customize the menus and menu items that appear in the header, create a new `base` subdirectory under your directory and copy [`server/config/custom_dc/custom/base/header.json`](https://github.com/datacommonsorg/website/blob/master/server/config/custom_dc/custom/base/header.json){: target="_blank"} there.
53+
```
54+
cd myproject
55+
mkdir base
56+
cp ../../../config/custom_dc/custom/base/header.json base/
57+
```
58+
59+
### Step 2: Set up your static assets environment
60+
61+
1. Create a new subdirectory under `static/custom_dc/` using the same name you created in step 1.
62+
```
63+
cd website/static/custom_dc
64+
mkdir myproject
65+
```
66+
1. Copy [`static/custom_dc/custom/overrides.css`](https://github.com/datacommonsorg/website/blob/master/static/custom_dc/custom/overrides.css){: target="_blank"} into this directory. You can use this file to define your own styles.
67+
```
68+
cp custom/overrides.css myproject/
69+
```
70+
1. Place your logo, image files and any custom Javascript and CSS files in this directory. In the relevant HTML template files (e.g. `base.html` etc.), be sure to add `script` and `link` elements to reference them. For example:
71+
```{% raw %}
72+
<head>
73+
...
74+
<script src={{url_for('static', filename='my_file.js', t=config['VERSION'])}} async></script>
75+
<link rel="stylesheet" href={{url_for('static', filename='css/my_file.css', t=config['VERSION'])}}>
76+
...
77+
</head>
78+
{% endraw %}```
79+
80+
### Step 3: Set up environment variables
81+
82+
1. In your `custom_dc/env.list` file, set the `FLASK_ENV` variable to the same name you created in step 1:
83+
```
84+
FLASK_ENV=myproject
85+
```
86+
1. Copy and rename the file [`server/app_env/custom.py`](https://github.com/datacommonsorg/website/blob/master/server/app_env/custom.py){: target="_blank"} to the same name.
87+
```
88+
cd website/server/app_env
89+
cp custom.py myproject.py
90+
```
91+
1. In this file, set the following variables:
92+
```
93+
NAME = "My Data Commons" # Used for browser title bar
94+
OVERRIDE_CSS_PATH = "/custom_dc/myproject/overrides.css"
95+
LOGO_PATH = "/custom_dc/myproject/logo.svg"
96+
```
97+
98+
> Tip: The `app_env/myproject.py` file overrides default options set in `app_env/_base.py`. You can add other variables you would like to override from that file.
99+
100+
## Simple customizations
101+
102+
The following are simple customizations you can make by editing HTML, CSS, and JSON files directly.
103+
104+
- Logo: Replace [`logo.svg`](https://github.com/datacommonsorg/website/blob/master/static/custom_dc/custom/logo.svg){: target="_blank"} with your own logo file.
105+
- Styles: Add new selectors and declaration blocks to [`overrides.css`](https://github.com/datacommonsorg/website/blob/master/static/custom_dc/custom/overrides.css){: target="_blank"}. (Note: The provided blocks control more than just styles, but content as well. Don't try to override them.)
106+
- Add a site-wide footer: Add elements to the `footer` block in [`base.html`](https://github.com/datacommonsorg/website/blob/master/server/templates/custom_dc/custom/base.html){: target="_blank"}. To style the footer using `overrides.css`, create a new CSS block. For example:
107+
```
108+
<!--base.html-->
109+
<footer id="my-footer">
110+
<p>Here is my footer!</p>
111+
</footer>
112+
```
113+
```
114+
/* overrides.css */
115+
#my-footer {
116+
border-top: 1px solid #efefef;
117+
background-color: green;
118+
}
119+
```
120+
- Header bar menus: In [`header.json`](https://github.com/datacommonsorg/website/blob/master/server/config/custom_dc/custom/base/header.json){: target="_blank"}, add, remove, or edit the default entries to change menus, text, items, section layout, and links.
121+
122+
- Add a search bar to the header: In [`base.html`](https://github.com/datacommonsorg/website/blob/master/server/templates/custom_dc/custom/base.html){: target="_blank"}, set this option:
123+
```{% raw %}
124+
{% set is_hide_header_search_bar = 'false' %}
125+
```{% endraw %}
126+
- Text and links on the Knowledge Graph landing page (`/browser`): Edit or replace the content in the `content` block of [browser_landing.html](https://github.com/datacommonsorg/website/blob/master/server/templates/custom_dc/custom/browser_landing.html){: target="_blank"}.
127+
128+
- Visualization tools (Map Explorer, Scatter Plot Explorer, Timeline Explorer) example chips: Add, remove, or modify default entries in the [`*_examples.json`](https://github.com/datacommonsorg/website/blob/master/server/templates/tools/){: target="_blank"} files as follows:
129+
130+
- Set `id` to any string you want.
131+
- Replace titleMessageId with title, and specify the text that you want to appear in the example chip. (Note: titleMessageId is only used if you are localizing your site, and is mutually exclusive with title.)
132+
- Set `url` to the full, URL-encoded path to the chart you would like to display.
133+
Here's an example:
134+
135+
```json
136+
{
137+
"id": "map_oecd_country_gender_wage_gap",
138+
"title": "Gender wage gap by OECD country",
139+
"url": "tools/map#%26sv%3Dgender_wage_gap%26pc%3D0%26denom%3DCount_Person%26pd%3DEarth%26ept%3DCountry"
140+
}
141+
```
142+
- Add more pages to the site: Add HTML templates to your `server/templates/` directory. They should extend `base.html` and set the required variables listed at the top of that file.
143+
144+
> **Note:** Currently, making changes to any of the files in the `static/` directory, even if you're testing locally, requires that you rebuild a local version of the repo to pick up the changes, as described in [Build a local image](/custom_dc/build_image.html#build-repo).
145+
146+
147+
{: #complex}
148+
## Complex customizations: header and homepage
149+
150+
The contents of the home page and site-wide header, defined in
151+
[`homepage.html`](https://github.com/datacommonsorg/website/blob/master/server/templates/custom_dc/custom/homepage.html){: target="_blank"} and [`base.html`](https://github.com/datacommonsorg/website/blob/master/server/templates/custom_dc/custom/base.html){: target="_blank"} respectively, are entirely generated by Javascript as React "apps". The Javascript is actually compiled at build time, using [Webpack](https://webpack.js.org/){: target="_blank"}. To make changes to these elements, you have two options:
152+
153+
- Override the default Javascript entirely to start from scratch. With this option, you can directly modify the template HTML and/or reuse JS you are already using in other parts of your site. However, you will essentially remove all the default content and start with a blank page and/or header. For example, if you override the home page JS, you'll need to provide code to generate the search bar. For the header, this is the only available option currently.
154+
- Modify the existing React component(s). In this way, you can mix and match the default content with your own. However, you'll need to code in Typescript and add build rules to Webpack to create your custom Javascript file(s). The Typescript is a separate Custom Data Commons component that you can copy and build. This option is only usable for the homepage.
155+
156+
### Option 1: Override default components
157+
158+
To remove the default header contents (logo, title, and menus), do the following:
159+
160+
1. In your copied `base.html` file, remove the header `main-header` ID (or rename to something else), and add HTML elements in the tags.
161+
```
162+
<header id="my-header">
163+
<p>Here is my header content!</p>
164+
</header>
165+
```
166+
1. If you have your own JS file(s), add them to your <code>static/custom_dc/<var>PROJECT_NAME</var></code> directory and add script elements to the head section of base.html. For example:
167+
```{% raw %}
168+
<head>
169+
...
170+
<script src={{url_for('static', filename='my_file.js', t=config['VERSION'])}} async></script>
171+
...
172+
</head>
173+
{% endraw %}```
174+
175+
1. To add styling for the header to `overrides.css`, add a new block for it:
176+
```
177+
#my-header {
178+
...
179+
}
180+
```
181+
182+
To remove the default home page main body (text, search bar and tools), do the following:
183+
1. In your copied `homepage.html` file, remove the `main-header` ID from the content block div (or rename it to something else), and add HTML elements in the tags.
184+
185+
```
186+
<div id="my-content">
187+
<p>Here is my home page content!</p>
188+
</div>
189+
```
190+
191+
1. If you have your own JS file(s), add them to your <code>static/custom_dc/<var>PROJECT_NAME</var></code> directory and add lines like this to the head section of `homepage.html`:
192+
```{% raw %}
193+
<head>
194+
...
195+
<script src={{url_for('static', filename='my_file.js', t=config['VERSION'])}} async></script>
196+
...
197+
</head>
198+
{% endraw %}```
199+
1. To add styling, add new selector blocks to `overrides.css`.
200+
201+
### Option 2: Modify default Javascript
202+
203+
> **Note**: Only use this procedure if you are familiar with React libraries and coding in Typescript.
204+
205+
To modify elements of the home page:
206+
207+
1. Copy the files [`static/js/apps/homepage/custom_dc_app.tsx`](https://github.com/datacommonsorg/website/blob/master/static/js/apps/homepage/custom_dc_app.tsx){: target="_blank"} and [`static/js/apps/homepage/main_custom_dc.ts`](https://github.com/datacommonsorg/website/blob/master/static/js/apps/homepage/main_custom_dc.ts){: target="_blank"} and give them different file names. For example:
208+
```
209+
cd website/static/js/apps/homepage
210+
cp custom_dc_app.tsx my_homepage.tsx
211+
cp main_custom_dc.ts my_main.ts
212+
```
213+
1. Edit [`static/webpack.config.js`](https://github.com/datacommonsorg/website/blob/master/static/webpack.config.js){: target="_blank"} to add another build entry:
214+
```
215+
my_homepage: [
216+
__dirname + "/js/apps/homepage/my_main.ts",
217+
__dirname + "/css/homepage.scss",
218+
]
219+
```
220+
221+
1. Edit the `.tsx` file to add or remove components.
222+
223+
1. In `base.html`, replace the `homepage_custom_dc.js` script file name with your new name. For example:
224+
```{% raw %}
225+
<head>
226+
...
227+
<script src={{url_for('static', filename='my_homepage.js', t=config['VERSION'])}} async></script>
228+
...
229+
</head>
230+
{% endraw %}```

custom_dc/quickstart.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,15 @@ cd website
105105
<td width="300"><a href="https://github.com/datacommonsorg/website/tree/master/custom_dc/sample" target="_blank"><code>custom_dc/sample/</code></a></td>
106106
<td>Sample data and config file (`config.json`) that can be added to a Custom Data Commons. This page describes the model and format of this data and how you can load and view it. </td>
107107
</tr>
108-
<tr>
109-
<td><a href="https://github.com/datacommonsorg/website/tree/master/server/templates/custom_dc/custom" target="_blank"><code>server/templates/custom_dc/custom/</code></a></td>
110-
<td>Contains customizable HTML files. To modify these, see <a href="custom_ui.html#html-templates">Customize HTML templates</a>.</td>
111-
</tr>
112-
<tr>
113-
<td><a href="https://github.com/datacommonsorg/website/tree/master/static/custom_dc/custom" target="_blank"><code>static/custom_dc/custom/</code></a></td>
114-
<td>Contains customizable CSS file and default logo. To modify the styles or replace the logo, see <a href="custom_ui.html#styles">Customize Javascript and styles</a>.</td>
115-
</tr>
116108
<tr>
117109
<td><a href="https://github.com/datacommonsorg/website/tree/master/deploy/terraform-custom-datacommons" target="_blank"><code>deploy/terraform-custom-datacommons</code></a></td>
118110
<td>Contains <a href="https://developer.hashicorp.com/terraform" target="_blank">Terraform</a> and convenience shell scripts for setting up your instance on Google Cloud Platform. See <a href="deploy_cloud.md">Deploy your custom instance to Google Cloud</a> for complete details.</td>
119111
</tr>
120112
</tbody>
121113
</table>
122114

115+
Additional files, that control the site user interface, are described in [Customize the site](custom_ui.md).
116+
123117
## Look at the sample data
124118

125119
Before you start up a Data Commons site, it's important to understand the basics of the data model that is expected in a custom Data Commons instance. Let's look at the sample data in the CSV files in the `custom_dc/sample/` folder. This data is from the Organisation for Economic Co-operation and Development (OECD): "per country data for annual average wages" and "gender wage gaps":

0 commit comments

Comments
 (0)