Spaces:
Sleeping
Sleeping
decentralizedsurvey
commited on
Commit
•
b1ca806
1
Parent(s):
6aa38d0
Upload 2 files
Browse files- app.py +79 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import web3
|
3 |
+
import requests
|
4 |
+
import pandas
|
5 |
+
|
6 |
+
# Connect to the Sepolia Ethereum blockchain
|
7 |
+
w3 = web3.Web3(web3.HTTPProvider(st.secrets["infura"]))
|
8 |
+
|
9 |
+
# Variables
|
10 |
+
ABI = '[ { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "string", "name": "data", "type": "string" } ], "name": "Store", "type": "event" }, { "inputs": [ { "internalType": "string", "name": "_IPFSHash", "type": "string" } ], "name": "storeHash", "outputs": [], "stateMutability": "nonpayable", "type": "function" } ]'
|
11 |
+
|
12 |
+
# Changing the App title
|
13 |
+
st.set_page_config(page_title="Survey Data Retriever",)
|
14 |
+
|
15 |
+
|
16 |
+
# The following code centralizes all the buttons
|
17 |
+
st.markdown("<style>.row-widget.stButton {text-align: center;}</style>", unsafe_allow_html=True)
|
18 |
+
|
19 |
+
# Title
|
20 |
+
st.title('Survey Data Retriever')
|
21 |
+
st.write("# ")
|
22 |
+
|
23 |
+
|
24 |
+
# Text field
|
25 |
+
contract_address = st.text_input(r"$\textsf{\Large Smart Contract Address}$", '0x42b76d8c32f914630627bf924bd1e06055673cf8')
|
26 |
+
|
27 |
+
# Button
|
28 |
+
button = st.button("Retrieve Survey Responses")
|
29 |
+
|
30 |
+
# Button is pressed
|
31 |
+
if button:
|
32 |
+
|
33 |
+
# Starts the progression bar
|
34 |
+
my_bar = st.progress(0, text= "Operation in progress. Please wait.")
|
35 |
+
|
36 |
+
# data frame with the data
|
37 |
+
df = pandas.DataFrame()
|
38 |
+
|
39 |
+
# Getting logs from ETH contract
|
40 |
+
contract = w3.eth.contract( address = contract_address, abi = ABI)
|
41 |
+
logs = contract.events.Store.get_logs(fromBlock= 4635673)
|
42 |
+
|
43 |
+
# Writing a label
|
44 |
+
st.write(r"$\textsf{\normalsize \textbf{IPFS Hashes}}$")
|
45 |
+
|
46 |
+
for i, log in enumerate(logs):
|
47 |
+
|
48 |
+
# Adjusting progression bard
|
49 |
+
my_bar.progress((i+1)/len(logs), text= "Operation in progress. Please wait.")
|
50 |
+
|
51 |
+
# Writing the hashes
|
52 |
+
st.write(f"**Hash**: {log.args.data}; **Block number**: {log.blockNumber}")
|
53 |
+
|
54 |
+
# Requesting data from IPFS using INFURA API
|
55 |
+
params = (('arg', log.args.data),)
|
56 |
+
response = requests.post('https://ipfs.infura.io:5001/api/v0/cat',
|
57 |
+
params=params,
|
58 |
+
auth=(st.secrets["username"], st.secrets["password"]))
|
59 |
+
|
60 |
+
# create a row for the values associated with the 'data' key
|
61 |
+
json_to_row = pandas.json_normalize(response.json())
|
62 |
+
|
63 |
+
# append data to empty data frame
|
64 |
+
df = pandas.concat([df,json_to_row], ignore_index=True)
|
65 |
+
|
66 |
+
# Printing df
|
67 |
+
st.write("# ")
|
68 |
+
st.write(r" $\textsf{\normalsize \textbf{Response Data}}$")
|
69 |
+
st.write(df)
|
70 |
+
|
71 |
+
# Download button
|
72 |
+
st.download_button("Press to Download",
|
73 |
+
df.to_csv(index=False).encode('utf-8'),
|
74 |
+
"responses.csv",
|
75 |
+
"text/csv",
|
76 |
+
key='download-csv')
|
77 |
+
|
78 |
+
# Clearing progression bar
|
79 |
+
my_bar.empty()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
web3==6.11.2
|
2 |
+
requests==2.31.0
|
3 |
+
streamlit==1.28.2
|
4 |
+
pandas==2.0.3
|