Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Poetry as package manager | added github actions for publishing upon release creation #6

Merged
merged 31 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f285e04
Added poetry and removed setup
gabrielfior Jan 7, 2025
3c44f03
Added github actions
gabrielfior Jan 7, 2025
64a0d13
Updated publish.yml
gabrielfior Jan 7, 2025
927455b
testing 1
gabrielfior Jan 7, 2025
7bc0fa8
testing 2
gabrielfior Jan 7, 2025
76aca7a
always run publish
gabrielfior Jan 7, 2025
e457915
react-scripts added
gabrielfior Jan 7, 2025
c1c7960
actions using yarn
gabrielfior Jan 7, 2025
099b09f
fixed node v
gabrielfior Jan 7, 2025
22dce65
missing yarn.lock
gabrielfior Jan 7, 2025
48fb18b
Making CI work
gabrielfior Jan 8, 2025
77810e0
Added new source
gabrielfior Jan 8, 2025
4fbc9d2
added lock
gabrielfior Jan 8, 2025
c3f8746
try new publish
gabrielfior Jan 8, 2025
b7780f1
typo
gabrielfior Jan 8, 2025
99fc42c
add testpypi username
gabrielfior Jan 8, 2025
ed5b400
update testpypi dist
gabrielfior Jan 8, 2025
4fe2f2b
back to test pypi
gabrielfior Jan 8, 2025
97180ae
debug
gabrielfior Jan 8, 2025
5d781df
user not username
gabrielfior Jan 8, 2025
e0fadf9
new version
gabrielfior Jan 8, 2025
7032f09
Added confirmed status to wallet component | returning status back to…
gabrielfior Jan 8, 2025
801987f
Updating workflows
gabrielfior Jan 8, 2025
0b278ad
Fixed CI
gabrielfior Jan 8, 2025
966c518
Caching poetry
gabrielfior Jan 8, 2025
475b819
Caching yarn
gabrielfior Jan 8, 2025
9bee798
Simplified workflows
gabrielfior Jan 8, 2025
4b7e14a
Further simplified CI
gabrielfior Jan 8, 2025
b37e42f
Final touches
gabrielfior Jan 8, 2025
5586301
Implementing PR caching changes
gabrielfior Jan 9, 2025
7ea7ef8
bumped poetry version
gabrielfior Jan 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/actions/python_prepare/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: "Prepare development environment"
description: "Set up Python and Node.js environments with caching"
runs:
using: "composite"
steps:
# Python setup
- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: 3.10.14

- name: Cache Poetry installation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea with cache!

I didn't use that in Github before, but from their repo, it seems like there needs to be something as

if: steps.cache-primes.outputs.cache-hit != 'true'

in the follow-up step. Or did you test that Poetry will quickly skip the installation by itself?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see latest changes and the corresponding CI run -> https://github.com/gnosis/python-web3-wallet-streamlit/actions/runs/12692379504?pr=6

I believe the cache is working now 🙏

uses: actions/cache@v3
with:
path: ~/.local
key: poetry-${{ runner.os }}

- name: Install Poetry
shell: bash
run: curl -sSL https://install.python-poetry.org | python3 -

- name: Cache Poetry dependencies
uses: actions/cache@v3
with:
path: .venv
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test that this cache stuff works/makes it faster? From CI it looks like virtualenv is installed elsewhere:

Run poetry install
Creating virtualenv python-web3-wallet-96aaq9Ev-py3.10 in /home/runner/.cache/pypoetry/virtualenvs

so I wonder what is this caching.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See CI run above, Python packages + node packages are installed only once, although the action inside python_prepare is called by both.

key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}

- name: Install Python dependencies
shell: bash
run: poetry install

# Node.js setup
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '20'

- name: Cache node_modules
uses: actions/cache@v3
with:
path: |
python_web3_wallet/frontend/node_modules
~/.cache/yarn
key: ${{ runner.os }}-yarn-${{ hashFiles('python_web3_wallet/frontend/yarn.lock') }}

- name: Install frontend dependencies
shell: bash
run: yarn install --cwd python_web3_wallet/frontend --immutable
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Python + frontend CI

on:
pull_request:
push:
branches: [main]
workflow_dispatch:

jobs:
build_frontend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/python_prepare
- name: Build Frontend
env:
CI: false # needed otherwise dependencies warnings stop the job
run: yarn --cwd python_web3_wallet/frontend build
- name: Cache frontend build
uses: actions/cache@v3
with:
path: python_web3_wallet/frontend/build
key: frontend-build-${{ github.sha }}

build_python:
needs: [build_frontend]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/python_prepare
- name: Restore frontend build
uses: actions/cache@v3
with:
path: python_web3_wallet/frontend/build
key: frontend-build-${{ github.sha }}
fail-on-cache-miss: true
- name: Run poetry build
run: poetry build -f wheel
64 changes: 64 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Python CD

on:
pull_request:
release:
types: [ published ]

jobs:
build:
if: github.event_name == 'release'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout Repository
uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Verify the tag version in the pyproject.toml
run: grep -q "version = \"${{ github.event.release.tag_name }}\"" pyproject.toml || exit 1
shell: bash
- name: Set Node.js 20.x
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'yarn'
- uses: ./.github/actions/python_prepare
- name: Build Frontend
env:
CI: false # needed otherwise dependencies warnings stop the job
run: yarn --cwd python_web3_wallet/frontend build
- name: Build a binary wheel and a source tarball
run: poetry build
- name: Store the distribution packages
uses: actions/upload-artifact@v4
with:
name: python-package-distributions
path: dist/

publish-to-pypi:
name: Publish to PyPI
needs:
- build
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/python-web3-wallet

permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing

steps:
- name: Download all the dists
uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/
- name: Publish distribution to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_TOKEN }}
verify-metadata: false
verbose: true
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
dist/
build/
.env
*.egg-info
*.egg-info
.idea/
1,395 changes: 1,395 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[tool.poetry]
name = "python-web3-wallet"
version = "0.0.11"
description = "Streamlit component that allows users to connect a wallet and send transactions with dynamic recipients and amounts"
authors = ["Gnosis AI <[email protected]>"]
license = "MIT"
gabrielfior marked this conversation as resolved.
Show resolved Hide resolved
readme = "README.md"
include = [
"python_web3_wallet/frontend/build/**/*"
]
packages = [
{ include = "python_web3_wallet" }
]

[tool.poetry.dependencies]
python = "^3.10"
streamlit = "^1.40.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

10 changes: 4 additions & 6 deletions python_web3_wallet/README
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ Run [app.py](app.py) with Streamlit - edit [PythonWeb3Wallet](frontend/src/Pytho
npm run build --prefix python_web3_wallet/frontend
```

2. Build python wheel
```
python setup.py sdist bdist_wheel
2. Publish Python package
```
# publish on testpy
python -m twine upload --repository testpypi dist/* --verbose
poetry publish -p {PYPI_TEST_TOKEN} -u "__token__" --build --repository testpypi
# upload to pypi
python -m twine upload dist/* --verbose
```
poetry publish -p {PYPI_TOKEN} -u "__token__" --build
```
3 changes: 2 additions & 1 deletion python_web3_wallet/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

import streamlit as st
st.title('My title')
c = component(recipient="0x07354C0aD12741E8F222eB439cFf4c01716cA627", amountInEther="0.00001", data='0x48656c6c6f20776f726c64')
window_close = component(recipient="0x07354C0aD12741E8F222eB439cFf4c01716cA627", amountInEther="0.00001", data='0x78dacb48cdc9c95728cf2fca4901001a0b045d')
st.write(f"window close {window_close}")
34 changes: 26 additions & 8 deletions python_web3_wallet/frontend/src/PythonWeb3Wallet.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { ConnectButton } from "@rainbow-me/rainbowkit";
import '@rainbow-me/rainbowkit/styles.css';
import React, { ReactElement, useEffect, useMemo, useState } from "react";
import {
ComponentProps,
Streamlit,
withStreamlitConnection,
ComponentProps,
} from "streamlit-component-lib";
import { type Hex, getAddress, parseEther } from 'viem';
import React, { useEffect, useMemo, useState, ReactElement } from "react"
import { ConnectButton } from "@rainbow-me/rainbowkit"
import '@rainbow-me/rainbowkit/styles.css';
import { useAccount, useSendTransaction, useWriteContract, useChainId, useWaitForTransactionReceipt, BaseError } from "wagmi";
import { BaseError, useAccount, useWaitForTransactionReceipt, useWriteContract } from "wagmi";
import { abi } from './abi';
import { AGENT_COMMUNICATION_CONTRACT } from "./constants";
import { waitForTransactionReceipt } from "viem/actions";
/**
* This is a React-based component template. The passed props are coming from the
* Streamlit library. Your custom args can be accessed via the `args` props.
Expand All @@ -20,12 +21,24 @@ function PythonWeb3Wallet({ args, disabled, theme }: ComponentProps): ReactEleme
const {
data: hash,
error,
isPending,
writeContractAsync
} = useWriteContract();
const { isLoading: isConfirming, isSuccess: isConfirmed } =
useWaitForTransactionReceipt({
hash
});

useEffect(() => {
if (isConfirmed) {
console.log('tx confirmed');
Streamlit.setComponentValue(isConfirmed);
}
}, [isConfirmed]);

const account = useAccount();

const [isFocused, setIsFocused] = useState(false)
const [isFocused, setIsFocused] = useState(false);

const style: React.CSSProperties = useMemo(() => {
if (!theme) return {}
Expand Down Expand Up @@ -59,6 +72,7 @@ function PythonWeb3Wallet({ args, disabled, theme }: ComponentProps): ReactEleme
}, {
onError: (err) => console.log(err),
});

console.log(`txHash ${txHash}`);
} catch {
// We simply pass here since errors already logged.
Expand All @@ -68,12 +82,13 @@ function PythonWeb3Wallet({ args, disabled, theme }: ComponentProps): ReactEleme
};


const isButtonDisabled = isPending || !account.isConnected;

return (
<>
<button
style={{
backgroundColor: !account.isConnected ? 'gray' : 'blue',
backgroundColor: isButtonDisabled ? 'gray' : 'blue',
color: 'white',
padding: '15px 30px',
marginBottom: '15px',
Expand All @@ -82,11 +97,14 @@ function PythonWeb3Wallet({ args, disabled, theme }: ComponentProps): ReactEleme
borderRadius: '10px',
}}
onClick={(sendMessage)}
disabled={!account.isConnected}
disabled={isButtonDisabled}
>
Send message to agent
</button>

{isConfirming && <div>Waiting for confirmation...</div>}
{isConfirmed && <div>Transaction confirmed.</div>}

{error && (
<div>Error: {(error as BaseError).shortMessage || error.message}</div>
)}
Expand Down
2 changes: 1 addition & 1 deletion python_web3_wallet/frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const queryClient = new QueryClient();

const config = getDefaultConfig({
appName: 'app',
projectId: process.env.REACT_APP_RAINBOW_WALLET_PROJECT_ID!,
projectId: process.env.REACT_APP_RAINBOW_PROJECT_ID!,
chains: [
gnosis,
//gnosisFoundryLocalhost
Expand Down
29 changes: 0 additions & 29 deletions setup.py

This file was deleted.

4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


Loading