You can find the complete guide on the Dune blog here.
This python script integrates with the Dune Analytics API to retrieve data about the top 10 NFT collections by volume within a specified date range. It prompts the user to input the start and end dates for the query, loads the DUNE API key from a .env
file, and runs the query using the dune_client
library. It then displays the results DataFrame and creates visualizations using Seaborn for better data representation.
You can find the sql query that this script runs in SQL QUERY/query_sql.sql
file.
The flowchart below outlines the process of the Python script designed to retrieve and display Ethereum NFT data from Dune, starting from loading the API key to graphing the trading volumes.
To run the script src/script.py
, you need to install the following dependencies:
-
dune-client: A Python client library for interacting with the Dune Analytics API.
- Installation:
pip install dune-client
- Installation:
-
dotenv: Loads environment variables from a
.env
file.- Installation:
pip install python-dotenv
- Installation:
-
seaborn: Python visualization library based on matplotlib.
- Installation:
pip install seaborn
- Installation:
-
matplotlib: Python plotting library.
- Installation:
pip install matplotlib
- Installation:
-
pandas: Data manipulation and analysis library.
- Installation:
pip install pandas
- Installation:
- Dune API Key: You need to obtain an API key from Dune Analytics and store it in
Dune API Key/dune-api-key.env
file.
Once you have installed the above dependencies and obtained your Dune API key, you can run the script successfully.
- The
load_dune_api_key()
function loads the DUNE API key from a.env
file using theload_dotenv
function from thedotenv
library.
- The
get_dune_client(api_key)
function creates and returns aDuneClient
instance using the provided API key.
- The
run_dune_query(client, query_id, start_date, end_date)
function runs a DUNE query to retrieve data within the specified date range. It constructs the query usingQueryBase
andQueryParameter
objects from thedune_client
library.
- The
get_user_input()
function prompts the user to input the start and end dates for the query. It validates the input format and ensures that the end date is after the start date.
- The
main()
function serves as the entry point of the script. It orchestrates the execution of other functions, including loading the API key, creating the DuneClient instance, getting user input, running the query, displaying the results DataFrame, and creating visualizations.
- The script uses Seaborn to create two visualizations:
- To run the script, execute it using a Python interpreter. Follow the prompts to input the start and end dates for the query.
- Ensure that the required libraries (
dune_client
,dotenv
,seaborn
,matplotlib
) are installed in your Python environment. - The result of the DUNE query is saved into a csv file in
Results/results.csv
.