Skip to content

Commit 7581646

Browse files
Add a new tutorial for newcomers with no Python experience
This commit adds a new "Tutorials" documentation section, including a new tutorial for newcomers with no Python experience. Co-authored-by: merlin-esser-frequenz <[email protected]> Signed-off-by: Leandro Lucarella <[email protected]>
1 parent e0f9821 commit 7581646

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

docs/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
* [Home](index.md)
2+
* [Tutorials](tutorials/)
23
* [API Reference](reference/)
34
* [Contributing](CONTRIBUTING.md)

docs/tutorials/newcomer.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# Starting from scratch
2+
3+
## Introduction
4+
5+
This tutorial is an opinionated guide intended for people with no experience working with Python and [Jupyter Notebooks](https://jupyter.org/), to help them get started quickly from scratch.
6+
7+
To get started, create a directory for your project and follow the steps below to set up your development environment and run the client.
8+
9+
## Install VS Code
10+
11+
Download and install **Visual Studio Code** from the [official website](https://code.visualstudio.com/).
12+
13+
Once installed, add the following extensions:
14+
15+
- [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python)
16+
- [Jupyter extension](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter)
17+
18+
These enable Python development and interactive notebooks within VS Code.
19+
20+
## Set Up a Virtual Environment
21+
22+
It is recommended to use a **virtual environment** for isolating dependencies.
23+
When using a virtual environment, you create something like a sandbox or containerized workspace that isolates dependencies and packages so they don’t conflict with the system or other projects. After creating the virtual environment, you can install all packages you need for the current project. To set up an initial virtual environment for using the Reporting API, go through the following steps:
24+
25+
### Linux / macOS
26+
27+
```bash
28+
### Go to your projects directory, open a terminal and run the following commands
29+
30+
# Create a virtual environment
31+
python3 -m venv .venv
32+
33+
# Activate the environment
34+
source .venv/bin/activate # when using bash
35+
source .venv/bin/activate.fish # when using fish
36+
37+
# Install the version of the reporting client you want to use
38+
pip install frequenz-client-reporting==0.18.0
39+
40+
# Install python-dotenv
41+
# --> Used to load environment variables from a `.env` file into the projects environment
42+
pip install python-dotenv
43+
44+
# Install ipkernel
45+
# --> Python execution backend for Jupyter
46+
pip install ipkernel
47+
48+
# Register the virtual environment as a kernel
49+
python -m ipykernel install --user --name=myenv --display-name "Python (myenv)"
50+
51+
# Install pandas
52+
pip install pandas
53+
54+
# Deactivate the environment
55+
deactivate
56+
```
57+
58+
### Windows (PowerShell)
59+
60+
> [!WARNING]
61+
> Windows is not officially supported **yet**, but in general things should just work. If you find any issues using the client in Windows, please report them.
62+
63+
```powershell
64+
### Go to your projects directory, open PowerShell and run the following commands
65+
66+
# Create a virtual environment
67+
python -m venv .venv
68+
69+
# Activate the environment
70+
.venv\Scripts\Activate.ps1
71+
72+
# Install the version of the reporting client you want to use
73+
pip install frequenz-client-reporting==0.18.0
74+
75+
# Install python-dotenv
76+
# --> Used to load environment variables from a `.env` file into the project's environment
77+
pip install python-dotenv
78+
79+
# Install ipykernel
80+
# --> Python execution backend for Jupyter
81+
pip install ipykernel
82+
83+
# Register the virtual environment as a kernel
84+
python -m ipykernel install --user --name=myenv --display-name "Python (myenv)"
85+
86+
# Install pandas
87+
pip install pandas
88+
89+
# Deactivate the environment (only when you are finished working with this project)
90+
#deactivate
91+
```
92+
93+
## API Keys
94+
95+
To access the API, a key has to be created via Frequenz Web UI and stored locally on your computer. For that go through the following steps:
96+
97+
### Create a `.env` file
98+
99+
When creating the API key, you will get two keys. One is the API key itself and the other one is the API secret. Go to your projects directory and create a text file named `.env` where you can store the API key and the API secret.
100+
Prepare the text file as follows:
101+
102+
```bash
103+
REPORTING_API_AUTH_KEY=
104+
REPORTING_API_SIGN_SECRET=
105+
```
106+
107+
### Create API Key via Frequenz Web UI
108+
109+
Log into your Frequenz Web UI account and click on your email-address on the bottom left. Then continue with `API keys` and `Create API key`. The created API key will be shown in the UI. This is the only time, the API key will be shown to you. If you lose it, you should remove the lost key from the UI and create a new one.
110+
Copy the API key and the API secret and add it to the `.env` file.
111+
112+
```bash
113+
REPORTING_API_AUTH_KEY=[your-api-key]
114+
REPORTING_API_SIGN_SECRET=[your-api-secret]
115+
```
116+
117+
### Testing the Client
118+
119+
Please also refer to source of the [CLI tool](https://github.com/frequenz-floss/frequenz-client-reporting-python/blob/v0.x.x/src/frequenz/client/reporting/cli/__main__.py)
120+
for a practical example of how to use the client.
121+
122+
## Initial Client and Notebook Setup
123+
124+
### Create a Notebook
125+
126+
Open your project's directory with VS Code. You should see your `.venv` and `.env` files in the directory when using VS Code.
127+
Create a new `.ipynb` file (Jupyter notebook file) in the directory. When selecting the empty ipynb file, you will find `Select Kernel` on the top right of the file. Click on it and select `Jupyter Kernel > Python (myenv)`. Now the notebook will use your virtual environment as an interpreter.
128+
129+
### Initialize the Client
130+
131+
To use the Reporting API client, you need to initialize it with the server URL and authentication credentials.
132+
The server URL should point to your Frequenz Reporting API instance, and you will need an authentication key and a signing secret, as described above.
133+
134+
> **SERVER URL:**
135+
> Please ask your service administrator for the SERVER URL
136+
137+
> **Security Note:**
138+
> Always keep your authentication key and signing secret secure. Do not hard-code them in your source code or share them publicly.
139+
140+
```python
141+
# Import basic packages
142+
from dotenv import load_dotenv
143+
from datetime import datetime, timedelta
144+
import os
145+
import pandas as pd
146+
147+
# Import Metrics and ReportingApiClient
148+
from frequenz.client.common.metric import Metric
149+
from frequenz.client.reporting import ReportingApiClient
150+
151+
# Read the API key from .env and sets it as environment variables in the current python process
152+
load_dotenv()
153+
154+
# Create Reporting API Client
155+
SERVER_URL = "grpc://replace-this-with-your-server-url:port"
156+
AUTH_KEY = os.environ['REPORTING_API_AUTH_KEY'].strip()
157+
SIGN_SECRET= os.environ['REPORTING_API_SIGN_SECRET'].strip()
158+
client = ReportingApiClient(server_url=SERVER_URL, auth_key=AUTH_KEY, sign_secret=SIGN_SECRET)
159+
```
160+
161+
## Next steps
162+
163+
You can now try out the code examples, like the [examples in the README](../index.md#query-metrics-for-a-single-microgrid-and-component).

0 commit comments

Comments
 (0)