Skip to content

Commit

Permalink
Merge pull request #15 from 0xPlaygrounds/refactor/contrib
Browse files Browse the repository at this point in the history
`contrib`: `plotly` and `dash` subpackages
  • Loading branch information
0xMochan authored Apr 13, 2023
2 parents 5a5f060 + f1d79e8 commit 16def22
Show file tree
Hide file tree
Showing 26 changed files with 782 additions and 609 deletions.
6 changes: 2 additions & 4 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
## Running the examples
Install subgrounds with your favorite Python dependency manager. E.g.: `pip install subgrounds`.

Run the examples, e.g.: `python bar_chart.py`
# Examples
> See [our docs](https://docs.playgrounds.network/examples/) for more examples!
20 changes: 20 additions & 0 deletions examples/aave_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from subgrounds import Subgrounds

sg = Subgrounds()

# Load
aave_v2 = sg.load_subgraph(
"https://api.thegraph.com/subgraphs/name/messari/aave-v2-ethereum"
)

# Construct the query
latest = aave_v2.Query.markets(
orderBy=aave_v2.Market.totalValueLockedUSD,
orderDirection="desc",
first=5,
)

# Return query to a dataframe
df = sg.query_df([latest.name, latest.totalValueLockedUSD])

print(df)
36 changes: 36 additions & 0 deletions examples/curve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from subgrounds import Subgrounds

sg = Subgrounds()

curve = sg.load_subgraph(
"https://api.thegraph.com/subgraphs/name/messari/curve-finance-ethereum"
)

# Partial FieldPath selecting the top 4 most traded pools on Curve
most_traded_pools = curve.Query.liquidityPools(
orderBy=curve.LiquidityPool.cumulativeVolumeUSD,
orderDirection="desc",
first=4,
)

# Partial FieldPath selecting the top 2 pools by daily total revenue of
# the top 4 most traded tokens.
# Mote that reuse of `most_traded_pools` in the partial FieldPath
most_traded_snapshots = most_traded_pools.dailySnapshots(
orderBy=curve.LiquidityPoolDailySnapshot.dailyTotalRevenue,
orderDirection="desc",
first=3,
)

# Querying:
# - the name of the top 4 most traded pools, their 2 most liquid
# pools' token symbols and their 2 most liquid pool's TVL in USD
df = sg.query_df(
[
most_traded_pools.name,
most_traded_snapshots.dailyVolumeUSD,
most_traded_snapshots.dailyTotalRevenueUSD,
]
)

print(df)
8 changes: 8 additions & 0 deletions examples/dash_apps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Dash Examples

## Instructions

```bash
pip install "subgrounds[dash]"
python bar_chart.py
```
6 changes: 3 additions & 3 deletions examples/bar_chart.py → examples/dash_apps/bar_chart.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import dash
from dash import html

from subgrounds.dash_wrappers import Graph
from subgrounds.plotly_wrappers import Bar, Figure
from subgrounds.subgrounds import Subgrounds
from subgrounds import Subgrounds
from subgrounds.contrib.dash import Graph
from subgrounds.contrib.plotly import Bar, Figure

sg = Subgrounds()
aaveV2 = sg.load_subgraph("https://api.thegraph.com/subgraphs/name/aave/protocol-v2")
Expand Down
8 changes: 3 additions & 5 deletions examples/dashboard.py → examples/dash_apps/dashboard.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import dash
from dash import html

# from subgrounds.components import AutoUpdate, BarChart, IndicatorWithChange
# from subgrounds.subgraph import Subgraph
from subgrounds.dash_wrappers import AutoUpdate, Graph
from subgrounds.plotly_wrappers import Bar, Figure, Indicator
from subgrounds.subgrounds import Subgrounds
from subgrounds.contrib.dash import AutoUpdate, Graph
from subgrounds.contrib.plotly import Bar, Figure, Indicator
from subgrounds import Subgrounds

sg = Subgrounds()
uniswapV2 = sg.load_subgraph(
Expand Down
6 changes: 3 additions & 3 deletions examples/indicator.py → examples/dash_apps/indicator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import dash
from dash import html

from subgrounds.dash_wrappers import Graph
from subgrounds.plotly_wrappers import Figure, Indicator
from subgrounds.subgrounds import Subgrounds
from subgrounds import Subgrounds
from subgrounds.contrib.dash import Graph
from subgrounds.contrib.plotly import Figure, Indicator

sg = Subgrounds()
uniswapV2 = sg.load_subgraph(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import dash
from dash import html

from subgrounds.dash_wrappers import AutoUpdate, Graph
from subgrounds.plotly_wrappers import Figure, Indicator
from subgrounds.subgrounds import Subgrounds
from subgrounds.contrib.dash import AutoUpdate, Graph
from subgrounds.contrib.plotly import Figure, Indicator
from subgrounds import Subgrounds

sg = Subgrounds()
uniswapV2 = sg.load_subgraph(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import dash
from dash import html

from subgrounds.dash_wrappers import Graph
from subgrounds.plotly_wrappers import Figure, Indicator, Scatter
from subgrounds.schema import TypeRef
from subgrounds.subgraph import SyntheticField
from subgrounds.subgrounds import Subgrounds
from subgrounds import Subgrounds, SyntheticField
from subgrounds.contrib.dash import Graph
from subgrounds.contrib.plotly import Figure, Indicator, Scatter

sg = Subgrounds()
olympusDAO = sg.load_subgraph(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
from datetime import datetime
from random import choice

import dash
from dash import html

from subgrounds.dash_wrappers import Graph
from subgrounds.plotly_wrappers import Figure, Indicator, Scatter
from subgrounds.schema import TypeRef
from subgrounds.subgraph import SyntheticField
from subgrounds.subgrounds import Subgrounds
from subgrounds import Subgrounds, SyntheticField
from subgrounds.contrib.dash import Graph
from subgrounds.contrib.plotly import Figure, Scatter

sg = Subgrounds()
olympusDAO = sg.load_subgraph(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import dash
from dash import html

from subgrounds.dash_wrappers import Graph
from subgrounds.plotly_wrappers import Figure, Scatter
from subgrounds import Subgrounds, SyntheticField
from subgrounds.contrib.dash import Graph
from subgrounds.contrib.plotly import Figure, Scatter
from subgrounds.schema import TypeRef
from subgrounds.subgraph import SyntheticField
from subgrounds.subgrounds import Subgrounds

sg = Subgrounds()
uniswapV2 = sg.load_subgraph(
Expand Down
5 changes: 2 additions & 3 deletions examples/table.py → examples/dash_apps/table.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import dash
from dash import html

from subgrounds.dash_wrappers import DataTable
from subgrounds.plotly_wrappers import Bar, Figure
from subgrounds.subgrounds import Subgrounds
from subgrounds import Subgrounds
from subgrounds.contrib.dash import DataTable

sg = Subgrounds()
uniswapV2 = sg.load_subgraph(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import dash
from dash import html

from subgrounds.dash_wrappers import AutoUpdate, DataTable
from subgrounds.subgrounds import Subgrounds
from subgrounds import Subgrounds
from subgrounds.contrib.dash import AutoUpdate, DataTable

sg = Subgrounds()
uniswapV2 = sg.load_subgraph(
Expand Down
78 changes: 0 additions & 78 deletions examples/double_query.py

This file was deleted.

39 changes: 0 additions & 39 deletions examples/indicator2.py

This file was deleted.

12 changes: 9 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dash = ["dash"]
# https://python-poetry.org/docs/managing-dependencies/#dependency-groups
[tool.poetry.group.dev.dependencies]
black = "^22.3.0"
deepdiff = "^6.2.1" # used for debugging data structures
deepdiff = "^6.2.1" # used for debugging data structures
ipykernel = "^6.13.0"
mypy = "^0.950"
nose2 = "^0.11.0"
Expand All @@ -36,8 +36,8 @@ python-semantic-release = "^7.33.1"
ruff = "^0.0.253"

[tool.poe.tasks]
format = { shell = "black subgrounds examples tests"}
check = { shell = "black subgrounds examples tests --check; ruff check subgrounds examples tests"}
format = { shell = "black subgrounds examples tests" }
check = { shell = "black subgrounds examples tests --check; ruff check subgrounds examples tests" }
develop = { shell = "mudkip develop" }
test = "pytest"
generate-api-docs = { shell = "sphinx-apidoc --output docs/api subgrounds --separate --force" }
Expand All @@ -50,6 +50,12 @@ version_toml = "pyproject.toml:tool.poetry.version"
major_on_zero = false
build_command = "poetry build"

[tool.ruff]

[tool.ruff.per-file-ignores]
"subgrounds/plotly_wrappers" = ["F405", "F403"]
"subgrounds/dash_wrappers" = ["F405", "F403"]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
17 changes: 17 additions & 0 deletions subgrounds/contrib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Subgrounds Contrib
> Extra parts of subgrounds that may not fit in the main package
## What is this?
Contrib is a niche concept in some libraries that represent extra / contributed content to a library that may not fit in the main package. This might be due to the maintainer not willing to maintain said content, the content being deemed too experimental, or perhaps it's unknown whether it's a "good idea" or not.

> Relevant [Stackoverflow](https://softwareengineering.stackexchange.com/questions/252053/whats-in-the-contrib-folder) post
For us, `subgrounds.contrib` will represent extra features and ideas with `subgrounds` that generally builds upon the core part of `subgrounds`. It allows us to add extensions or features to other libraries (such as `plotly`) without *relying* on the library as a dependency. We might add new concepts to this subpackage in the future, so look out!

## What's currently here?

### Plotly
Originally located in `subgrounds.plotly_wrappers`, `subgrounds.contrib.plotly` contains helpful wrappers on `plotly` objects that allow you to use `FieldPaths` directly without creating a `pandas` `DataFrame`.

### Dash
Originally located in `subgrounds.dash_wrappers`, `subgrounds.contrib.dash` contains helpful wrappers on `dash` objects that allow you to use other wrapped visualization objects (currently `subgrounds.contrib.plotly`) in `dash` apps without creating `pandas` `DataFrame`s.
15 changes: 15 additions & 0 deletions subgrounds/contrib/dash/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Subgrounds Dash Components
Extending dash components to be able to understand subgrounds logic. This includes other
extended components of other libraries such as `plotly`.
"""

from .abcs import Refreshable
from .components import AutoUpdate, DataTable, Graph

__all__ = [
"Refreshable",
"Graph",
"DataTable",
"AutoUpdate",
]
Loading

0 comments on commit 16def22

Please sign in to comment.