Skip to content

Commit

Permalink
Merge pull request #105 from hs3city/switch-finance-workflow-to-nix
Browse files Browse the repository at this point in the history
Switch finance workflow to Nix
  • Loading branch information
DoomHammer authored Jul 2, 2024
2 parents b490e62 + 737f8be commit ab2032b
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 19 deletions.
15 changes: 7 additions & 8 deletions .github/workflows/finance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@ name: "Update financial dashboard"
on:
push:
paths:
- 'automation/finanse/finanse.csv'
- '.github/workflows/finance.yaml'
- "automation/finanse/*"
- ".github/workflows/finance.yaml"

jobs:
dashboard:
runs-on: ubuntu-latest
name: Update financial dashboard
steps:
- uses: actions/[email protected]
- uses: actions/[email protected]
with:
python-version: '3.10'
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v27
- name: Update dashboard
run: |
cd automation/finanse
pip install -r requirements.txt
python dashboard.py
nix build
cp result ../../layouts/shortcodes/finanse.html
nix flake check
git add ../../layouts/shortcodes/finanse.html
- name: Commit Changes
uses: stefanzweifel/[email protected]
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ hugo.linux
hugo.darwin

node_modules

automation/finanse/result
18 changes: 11 additions & 7 deletions automation/finanse/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@
from bs4 import BeautifulSoup


def export_dashboard_cashflow(source_path, offline=False, standalone=False):
def export_dashboard_cashflow(source_path, dest_path="", offline=False, standalone=False):
"""
Returns HTML file with cashflow plot
Params
------
source_path : path with finanse.csv and current index.html included
dest_path : path to which the resulting file will be written
offline : if True returns html with plotly.js included (+3MB)
if False plotly.js requires internet connection to load
standalone : if True returns full html with html tag
if False returns a string containing a single <div>
"""

if dest_path == "":
dest_path = source_path
source_file = os.path.join(source_path, "finanse.csv")
output_file = os.path.join(source_path, "../../layouts/shortcodes/finanse.html")
output_file = os.path.join(dest_path, "finanse.html")

include_plotlyjs = "cdn"
if offline:
Expand Down Expand Up @@ -88,16 +91,16 @@ def export_dashboard_cashflow(source_path, offline=False, standalone=False):
fig.write_html(output_file, include_plotlyjs=include_plotlyjs, full_html=standalone)


def update_html(source_path):
def update_html(source_path, dest_path):

with open(
os.path.join(source_path, "../../layouts/shortcodes/finanse.html"),
os.path.join(dest_path, "finanse.html"),
encoding="utf-8",
) as cashflow:
soup_cashflow = BeautifulSoup(cashflow, "html.parser")

with open(
os.path.join(source_path, "../../layouts/shortcodes/finanse.html"),
os.path.join(dest_path, "finanse.html"),
mode="w",
encoding="utf-8",
) as output:
Expand All @@ -108,6 +111,7 @@ def update_html(source_path):

parser = argparse.ArgumentParser()
parser.add_argument("--source_path", type=str, default="")
parser.add_argument("--dest_path", type=str, default="")
parser.add_argument(
"--offline", default=False, action=argparse.BooleanOptionalAction
)
Expand All @@ -117,6 +121,6 @@ def update_html(source_path):

args = parser.parse_args()

export_dashboard_cashflow(args.source_path, args.offline, args.standalone)
export_dashboard_cashflow(args.source_path, args.dest_path, args.offline, args.standalone)
update_html(args.source_path, args.dest_path)
print("Graph plotted and exported successfully to HTML")
update_html(args.source_path)
130 changes: 130 additions & 0 deletions automation/finanse/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions automation/finanse/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
description = "Construct development shell from requirements.txt";
inputs = {

nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05-small";
flake-utils.url = "github:numtide/flake-utils";

pyproject-nix.url = "github:nix-community/pyproject.nix";
pyproject-nix.inputs.nixpkgs.follows = "nixpkgs";
};

outputs =
{
self,
nixpkgs,
flake-utils,
pyproject-nix,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
project = pyproject-nix.lib.project.loadRequirementsTxt { projectRoot = ./.; };

pkgs = import nixpkgs { inherit system; };
python = pkgs.python3;

pythonEnv =
#assert project.validators.validateVersionConstraints { inherit python; } == { }; (
pkgs.python3.withPackages (project.renderers.withPackages { inherit python; })
#);
;

in
with pkgs;
{
packages.dashboard = pkgs.stdenv.mkDerivation {
name = "dashboard";
src = ./.;
buildPhase = "${pythonEnv}/bin/python ./dashboard.py --source_path=./. --dest_path=./.";
installPhase = "cp ./finanse.html $out";
};
defaultPackage = self.packages.${system}.dashboard;
devShells.default = mkShell { buildInputs = [ pythonEnv ]; };
}
);
}
6 changes: 3 additions & 3 deletions automation/finanse/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pandas==1.3.4
plotly==5.3.1
beautifulsoup4==4.9.3
pandas~=1.3.4
plotly~=5.3.1
beautifulsoup4~=4.9.3
2 changes: 1 addition & 1 deletion layouts/shortcodes/finanse.html

Large diffs are not rendered by default.

0 comments on commit ab2032b

Please sign in to comment.