diff --git a/.github/workflows/finance.yaml b/.github/workflows/finance.yaml index 90972a4..39a29ea 100644 --- a/.github/workflows/finance.yaml +++ b/.github/workflows/finance.yaml @@ -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/checkout@v2.5.0 - - uses: actions/setup-python@v4.3.0 - 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/git-auto-commit-action@v4.15.3 diff --git a/.gitignore b/.gitignore index 974d6a2..0dcf0e1 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ hugo.linux hugo.darwin node_modules + +automation/finanse/result diff --git a/automation/finanse/dashboard.py b/automation/finanse/dashboard.py index 1333407..3aa56c1 100644 --- a/automation/finanse/dashboard.py +++ b/automation/finanse/dashboard.py @@ -5,13 +5,14 @@ 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 @@ -19,8 +20,10 @@ def export_dashboard_cashflow(source_path, offline=False, standalone=False): """ + 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: @@ -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: @@ -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 ) @@ -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) diff --git a/automation/finanse/flake.lock b/automation/finanse/flake.lock new file mode 100644 index 0000000..24a8091 --- /dev/null +++ b/automation/finanse/flake.lock @@ -0,0 +1,130 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "mdbook-nixdoc": { + "inputs": { + "nix-github-actions": [ + "pyproject-nix", + "nix-github-actions" + ], + "nixpkgs": [ + "pyproject-nix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1708395692, + "narHash": "sha256-smf0VmxGbjJDZqKvxxG3ZVqubgbVwAWG26wPo+BT/A0=", + "owner": "adisbladis", + "repo": "mdbook-nixdoc", + "rev": "d6a71b114b9221c0b4f20d31b81766d072cc26be", + "type": "github" + }, + "original": { + "owner": "adisbladis", + "repo": "mdbook-nixdoc", + "type": "github" + } + }, + "nix-github-actions": { + "inputs": { + "nixpkgs": [ + "pyproject-nix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1703863825, + "narHash": "sha256-rXwqjtwiGKJheXB43ybM8NwWB8rO2dSRrEqes0S7F5Y=", + "owner": "nix-community", + "repo": "nix-github-actions", + "rev": "5163432afc817cf8bd1f031418d1869e4c9d5547", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nix-github-actions", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1719176307, + "narHash": "sha256-r8eUJqKlq2xQmK95s1Q9+OwV6Py/pPbDN8T6V4GLP8E=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "5defcb4b0d6c286e8b4b2869cf38efb4cdeb5bf4", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-24.05-small", + "repo": "nixpkgs", + "type": "github" + } + }, + "pyproject-nix": { + "inputs": { + "mdbook-nixdoc": "mdbook-nixdoc", + "nix-github-actions": "nix-github-actions", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1716218469, + "narHash": "sha256-w2djd2mxQhBIqDhCVSW544vFxGgCFX6vm/cpk1Btzg8=", + "owner": "nix-community", + "repo": "pyproject.nix", + "rev": "298d55c4122a9a2177fd56cb18796d62daf386c5", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "pyproject.nix", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "pyproject-nix": "pyproject-nix" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/automation/finanse/flake.nix b/automation/finanse/flake.nix new file mode 100644 index 0000000..e1c761d --- /dev/null +++ b/automation/finanse/flake.nix @@ -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 ]; }; + } + ); +} diff --git a/automation/finanse/requirements.txt b/automation/finanse/requirements.txt index 0a8ff0a..77f932c 100644 --- a/automation/finanse/requirements.txt +++ b/automation/finanse/requirements.txt @@ -1,3 +1,3 @@ -pandas==1.3.4 -plotly==5.3.1 -beautifulsoup4==4.9.3 \ No newline at end of file +pandas~=1.3.4 +plotly~=5.3.1 +beautifulsoup4~=4.9.3 diff --git a/layouts/shortcodes/finanse.html b/layouts/shortcodes/finanse.html index 723e95d..c4b5b20 100644 --- a/layouts/shortcodes/finanse.html +++ b/layouts/shortcodes/finanse.html @@ -1,2 +1,2 @@