Skip to content

Commit 268371b

Browse files
authored
Merge branch 'main' into python-plotly-graph-carpet
2 parents 4aa0ca3 + ec410ba commit 268371b

File tree

5 files changed

+276
-0
lines changed

5 files changed

+276
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
Title: '.querySelectorAll()'
3+
Description: 'Returns a static (non-live) NodeList of all elements in the document that match the given CSS selectors.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Web Development'
7+
Tags:
8+
- 'Methods'
9+
- 'Node'
10+
- 'Selectors'
11+
CatalogContent:
12+
- 'introduction-to-javascript'
13+
- 'paths/front-end-engineer-career-path'
14+
---
15+
16+
In JavaScript, the **`.querySelectorAll()`** method under the `document` object returns a static (not live) `NodeList` of all elements that match the given group of [selectors](https://www.codecademy.com/resources/docs/css/selectors).
17+
18+
## Syntax
19+
20+
```pseudo
21+
document.querySelectorAll(selectors);
22+
```
23+
24+
- `selectors`: Represents a string containing one or more CSS selectors used to match elements in the document. It follows the same rules as CSS selectors and can include:
25+
- Type selectors (`div`, `p`, `span`)
26+
- Class selectors (`.class-name`)
27+
- ID selectors (`#id-name`)
28+
- Attribute selectors (`[type="text"]`, `[disabled]`)
29+
- Combinations (`div p`, `.container > p`, `ul > li:first-child`)
30+
31+
## Examples
32+
33+
### Example 1
34+
35+
In this example, a `NodeList` of all `<p>` elements in the document is obtained:
36+
37+
```js
38+
const matches = document.querySelectorAll('p');
39+
```
40+
41+
### Example 2
42+
43+
The following example returns a list of all `<div>` elements in the document with a class of either `note` or `alert`:
44+
45+
```js
46+
const matches = document.querySelectorAll('div.note, div.alert');
47+
```
48+
49+
### Example 3
50+
51+
In this example, a list of `<p>` elements is obtained, whose immediate parent is a `<div>` with the class `highlighted`, and which are inside a container with the ID `test`:
52+
53+
```js
54+
const container = document.querySelector('#test');
55+
const matches = container.querySelectorAll('div.highlighted > p');
56+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
Title: '.removeChild()'
3+
Description: 'Removes a given child node from the DOM and returns the removed node.'
4+
Subjects:
5+
- 'Web Development'
6+
- 'Web Design'
7+
Tags:
8+
- 'DOM'
9+
- 'ES6'
10+
- 'Arguments'
11+
- 'Functions'
12+
CatalogContent:
13+
- 'introduction-to-javascript'
14+
- 'paths/front-end-engineer-career-path'
15+
---
16+
17+
The **`.removeChild()`** method in JavaScript is a key DOM (Document Object Model) manipulation tool that removes a specified child node from the DOM tree. It enables developers to dynamically modify a webpage's structure by removing elements from their parent nodes. When called on a parent node with a child node argument, it removes that child from the DOM and returns the removed node. The returned node can still be used for other purposes, such as being inserted elsewhere in the DOM.
18+
19+
## Syntax
20+
21+
```pseudo
22+
parentNode.removeChild(childNode);
23+
```
24+
25+
- `parentNode`: The parent DOM node from which the child will be removed.
26+
- `childNode`: The node to remove (must be a child of the parent node).
27+
28+
### Notes
29+
30+
- The removed node still exists in memory and can be reused, though it's no longer in the DOM.
31+
- If the specified child is not a child of the parent node, the method will throw an error.
32+
- The node must be a direct child of the parent node.
33+
34+
## Example
35+
36+
The following example shows how to use `.removeChild()` to remove a paragraph from a `<div>`.
37+
38+
Here's the HTML code:
39+
40+
```html
41+
<div id="container">
42+
<p id="removeme">This paragraph will be removed!</p>
43+
</div>
44+
```
45+
46+
Here's the JavaScript code:
47+
48+
```js
49+
// Select the parent node
50+
const parentDiv = document.getElementById('container');
51+
52+
// Select the paragraph to remove
53+
const paragraphToRemove = document.getElementById('removeme');
54+
55+
// Remove the paragraph
56+
const removedParagraph = parentDiv.removeChild(paragraphToRemove);
57+
58+
// Print the removed paragraph
59+
console.log(removedParagraph.textContent);
60+
```
61+
62+
The above code produces the following output:
63+
64+
```shell
65+
"This paragraph will be removed!"
66+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
Title: '.Sankey()'
3+
Description: 'Creates Sankey diagrams, which visualize flow between categories using nodes and links.'
4+
Subjects:
5+
- 'Data Science'
6+
- 'Data Visualization'
7+
Tags:
8+
- 'Charts'
9+
- 'Graphics'
10+
- 'Plotly'
11+
- 'Python'
12+
CatalogContent:
13+
- 'learn-python-3'
14+
- 'paths/data-science'
15+
---
16+
17+
**`.Sankey()`** is a method in Plotly's [`graph_objects`](https://www.codecademy.com/resources/docs/plotly/graph-objects) module that creates Sankey diagrams, which visualize the flow between different values. The connected elements are called nodes, and the connections between them are links, where the width of each link represents the quantity of flow.
18+
19+
## Syntax
20+
21+
```pseudo
22+
plotly.graph_objects.Sankey(node=None, link=None, arrangement = 'snap', orientation = 'h', valueformat = None, ...)
23+
```
24+
25+
- `node`: A dictionary that defines the properties of the nodes in the Sankey diagram. It has the following keys:
26+
- `label`: Names of the nodes, where the order corresponds to their indices used in `link`.
27+
- `color`: A string or an array of strings specifying the color of each node. If a single string is provided, all nodes will have the same color. If an array is provided, each element defines the color of the node with the same index.
28+
- `link`: A dictionary that defines the links (connections) between nodes and their flow values. It contains the following keys:
29+
- `source`: An array of numerical indices. Each index specifies the source node of a link. The numerical index refers to the position of the node's name in the `label` array.
30+
- `target`: An array of numerical indices. Each index specifies the target node of a link. The numerical index refers to the position of the node's name in the `label` array.
31+
- `value`: An array of numerical values. Each value represents the flow quantity associated with a specific link. The order of values corresponds to the order of the source-target pairs.
32+
33+
> **Note:** The `source`, `target`, and `value` arrays must have the same length.
34+
35+
- `arrangement`: Sets the arrangement of the nodes in the Sankey diagram. The possible values are: `snap`, `perpendicular`, `freeform`, and `fixed`. The default value is `snap`.
36+
- `orientation`: Determines whether the Sankey diagram is displayed horizontally or vertically. The possible values are: `h` for horizontal, and `v` for vertical. The default value is `h`.
37+
- `valueformat`: Sets the numerical format for flow values, using d3-format syntax.
38+
39+
> **Note:** There are many additional, optional parameters that are not listed here, as indicated by the ellipsis (`...`) in the syntax.
40+
41+
## Example
42+
43+
This code displays a Sankey diagram, illustrating the advertising cash flow through its nodes, and links:
44+
45+
```py
46+
import plotly.graph_objects as go
47+
48+
# Define the data for the Sankey diagram (Advertising Cash Flow).
49+
data = {
50+
'source': ['Ad Campaign', 'Social Media', 'Search Engines', 'Referrals', 'Social Media', 'Search Engines', 'Referrals', 'Social Media', 'Search Engines', 'Referrals'],
51+
'target': ['Clicks', 'Clicks', 'Clicks', 'Clicks', 'Leads', 'Leads', 'Leads', 'Conversions', 'Conversions', 'Conversions'],
52+
'value': [500, 300, 200, 100, 150, 80, 40, 60, 30, 10]
53+
}
54+
55+
# Create a list of unique nodes.
56+
all_nodes = data['source'] + data['target']
57+
58+
# Create a dictionary that links the name of the node to its index.
59+
node_to_index = {node: i for i, node in enumerate(all_nodes)}
60+
61+
# Convert source, and target names to indices.
62+
source_indices = [node_to_index[source] for source in data['source']]
63+
target_indices = [node_to_index[target] for target in data['target']]
64+
65+
# Create the Sankey diagram.
66+
fig = go.Figure(data=[go.Sankey(
67+
node=dict(
68+
label=all_nodes,
69+
pad=20, # Add padding between nodes.
70+
thickness=10, # Define the thickness of the nodes.
71+
line=dict(color="black", width=0.5) # Add a border to the nodes.
72+
),
73+
link=dict(
74+
source=source_indices,
75+
target=target_indices,
76+
value=data['value'],
77+
color=['lightblue', 'lightgreen', 'lightcoral', 'indigo', 'turquoise', 'mediumvioletred', 'darkorange', 'yellowgreen', 'dodgerblue', 'lightblue'], # Define the color of the links.
78+
line=dict(color='black', width=0.2) # Define the border of the links.
79+
),
80+
arrangement='snap', # Set the arrangement of the nodes.
81+
orientation='h' # Set the orientation of the diagram.
82+
)])
83+
84+
# Update layout to add a title.
85+
fig.update_layout(title_text="Advertising Cash Flow", font_size=10)
86+
87+
# Display the plot.
88+
fig.show()
89+
```
90+
91+
This example results in the following output:
92+
93+
![The output will be a Sankey diagram.](https://raw.githubusercontent.com/Codecademy/docs/main/media/sankey-cash-flow.png)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
Title: '.acosh()'
3+
Description: 'Computes the inverse hyperbolic cosine of elements in a PyTorch tensor.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Deep Learning'
9+
- 'Programming'
10+
- 'PyTorch'
11+
CatalogContent:
12+
- 'intro-to-py-torch-and-neural-networks'
13+
- 'paths/data-science'
14+
---
15+
16+
The **`.acosh()`** method in PyTorch computes the inverse hyperbolic cosine of each element in a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors). This operation is applied element-wise and is commonly used in scientific computations involving hyperbolic functions.
17+
18+
## Syntax
19+
20+
```pseudo
21+
torch.acosh(input, *, out=None) → Tensor
22+
```
23+
24+
**Parameters:**
25+
26+
- `input`: The input tensor containing values greater than or equal to 1.
27+
- `out` (Optional): The output tensor to store the result. If not provided, a new tensor is created.
28+
29+
**Returns:**
30+
31+
A tensor with the inverse hyperbolic cosine of each element from the input tensor.
32+
33+
## Example
34+
35+
This example shows how to use `.acosh()` to compute the inverse hyperbolic cosine of a tensor:
36+
37+
```py
38+
import torch
39+
40+
# Define a tensor with values >= 1
41+
input_tensor = torch.tensor([1.0, 2.0, 3.0, 10.0])
42+
43+
# Compute the inverse hyperbolic cosine
44+
output_tensor = torch.acosh(input_tensor)
45+
46+
print("Input Tensor:")
47+
print(input_tensor)
48+
49+
print("\nOutput Tensor:")
50+
print(output_tensor)
51+
```
52+
53+
This example results in the following output:
54+
55+
```shell
56+
Input Tensor:
57+
tensor([ 1., 2., 3., 10.])
58+
59+
Output Tensor:
60+
tensor([0.0000, 1.3169, 1.7627, 2.9932])
61+
```

media/sankey-cash-flow.png

56.5 KB
Loading

0 commit comments

Comments
 (0)