Spaces:
Sleeping
Sleeping
[feat] first commit
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
from tools import time_ago, get_skill_datas
|
5 |
+
|
6 |
+
skills = get_skill_datas()
|
7 |
+
|
8 |
+
params = st.experimental_get_query_params()
|
9 |
+
|
10 |
+
|
11 |
+
st.session_state.skills = skills
|
12 |
+
params = st.experimental_get_query_params()
|
13 |
+
st.sidebar.link_button("Home", url="?page=main", use_container_width=True)
|
14 |
+
for skill in skills:
|
15 |
+
st.sidebar.link_button(f"{skill['skill_name']}", url=f"?page=detail_page&skill_name={skill['skill_name']}", use_container_width=True)
|
16 |
+
|
17 |
+
|
18 |
+
def main_page(skills):
|
19 |
+
for skill in skills:
|
20 |
+
with st.expander(f"{skill['skill_metadata']['author']}/{skill['skill_name']}", expanded=True):
|
21 |
+
tags = "`{}`".format("`\t\t`".join(skill['skill_tags']))
|
22 |
+
st.markdown(f"{tags}")
|
23 |
+
st.markdown(f"**<a href='?page=detail_page&skill_name={skill['skill_name']}' style='font-size: 22px;' target='_self'>{skill['skill_metadata']['author']}/{skill['skill_name']}</a>**", unsafe_allow_html=True)
|
24 |
+
st.markdown(f">**{skill['skill_description']}**")
|
25 |
+
usage = f"from {skill['skill_metadata']['author']}.skill_library.skill_code import {skill['skill_name']}\n{skill['Usage']}"
|
26 |
+
st.code(usage, language=skill['skill_program_language'])
|
27 |
+
st.markdown(f"""```Updated {time_ago(skill['skill_metadata']['updated_at'])}```""")
|
28 |
+
def detail_page(skills):
|
29 |
+
skill = next((skill for skill in skills if skill['skill_name'] == st.session_state.selected_skill_name), None)
|
30 |
+
|
31 |
+
st.markdown("""<a href='?page=main' target='_self'>
|
32 |
+
<button style='color: #4CAF50; cursor: pointer; border: 1px solid #4CAF50; border-radius: 5px; width: 10%; '>
|
33 |
+
Home
|
34 |
+
</button>
|
35 |
+
</a>"""
|
36 |
+
, unsafe_allow_html=True)
|
37 |
+
st.title(skill['skill_name'])
|
38 |
+
tags = "`{}`".format("`\t\t`".join(skill['skill_tags']))
|
39 |
+
st.markdown(f"{tags}")
|
40 |
+
st.markdown(f"""```Updated {time_ago(skill['skill_metadata']['updated_at'])}``` ```ver {skill['skill_metadata']['version']}```""")
|
41 |
+
st.markdown(f"## Description")
|
42 |
+
st.markdown(f"{skill['skill_description']}")
|
43 |
+
st.markdown("## Usage")
|
44 |
+
usage = f"from {skill['skill_metadata']['author']}.skill_library.skill_code import {skill['skill_name']}\n{skill['Usage']}"
|
45 |
+
st.code(usage, language=skill['skill_program_language'])
|
46 |
+
with st.expander(f"Parameters", expanded=False):
|
47 |
+
st.markdown("## Parameters")
|
48 |
+
st.code(f"{skill['Parameters']}", language=skill['skill_program_language'])
|
49 |
+
st.markdown("## Returns")
|
50 |
+
st.code(f"{skill['Returns']}", language=skill['skill_program_language'])
|
51 |
+
st.markdown("## Dependencies")
|
52 |
+
st.code(f"{skill['skill_dependencies']}", language=skill['skill_program_language'])
|
53 |
+
with st.expander(f"Code", expanded=False):
|
54 |
+
st.code(f"{skill['skill_code']}", language=skill['skill_program_language'])
|
55 |
+
|
56 |
+
if 'page' in params:
|
57 |
+
st.session_state.page = params['page'][0]
|
58 |
+
if st.session_state.page == 'detail_page' and 'skill_name' in params:
|
59 |
+
st.session_state.selected_skill_name = params['skill_name'][0]
|
60 |
+
else:
|
61 |
+
st.session_state.page = 'main'
|
62 |
+
|
63 |
+
if st.session_state.page == 'main':
|
64 |
+
main_page(skills)
|
65 |
+
elif st.session_state.page == 'detail_page':
|
66 |
+
detail_page(skills)
|
tools.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
from datetime import datetime
|
4 |
+
import re
|
5 |
+
|
6 |
+
def get_skill_files():
|
7 |
+
skill_files = []
|
8 |
+
skill_doc_files = []
|
9 |
+
for root, dirs, files in os.walk("."):
|
10 |
+
if "skill.json" in files:
|
11 |
+
skill_files.append(os.path.join(root, "skill.json"))
|
12 |
+
if "skill_doc.md" in files:
|
13 |
+
skill_doc_files.append(os.path.join(root, "skill_doc.md"))
|
14 |
+
return skill_files, skill_doc_files
|
15 |
+
|
16 |
+
def get_skill_data(file_path):
|
17 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
18 |
+
data = json.load(f)
|
19 |
+
return data
|
20 |
+
|
21 |
+
def get_skill_doc_data(file_path):
|
22 |
+
if not os.path.exists(file_path):
|
23 |
+
return None
|
24 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
25 |
+
lines = file.readlines()
|
26 |
+
title = lines[0].strip().replace("# ", "") if lines else None
|
27 |
+
content = "".join(lines)
|
28 |
+
|
29 |
+
# Regular expressions to extract the desired sections
|
30 |
+
usage_pattern = r"# Usage:\n(.*?)\n\n"
|
31 |
+
parameters_pattern = r"# Parameters:\n(.*?)\n\n"
|
32 |
+
returns_pattern = r"# Returns:\n(.*?)(\n\n|$|#)"
|
33 |
+
|
34 |
+
usage = re.search(usage_pattern, content, re.DOTALL)
|
35 |
+
parameters = re.search(parameters_pattern, content, re.DOTALL)
|
36 |
+
returns_ = re.search(returns_pattern, content, re.DOTALL)
|
37 |
+
|
38 |
+
# Extract the matched content or set to None if not found
|
39 |
+
usage_content = usage.group(1).strip() if usage else None
|
40 |
+
parameters_content = parameters.group(1).strip() if parameters else None
|
41 |
+
returns_content = returns_.group(1).strip() if returns_ else None
|
42 |
+
|
43 |
+
return {
|
44 |
+
"Title": title,
|
45 |
+
"Usage": usage_content,
|
46 |
+
"Parameters": parameters_content,
|
47 |
+
"Returns": returns_content
|
48 |
+
}
|
49 |
+
|
50 |
+
def get_skill_datas():
|
51 |
+
skill_files, skill_doc_files = get_skill_files()
|
52 |
+
skills_datas=[]
|
53 |
+
for i, file in enumerate(skill_files):
|
54 |
+
skill_doc_file = skill_doc_files[i]
|
55 |
+
skill = get_skill_data(file)
|
56 |
+
skill_doc = get_skill_doc_data(skill_doc_file)
|
57 |
+
if skill_doc:
|
58 |
+
skill.update(skill_doc)
|
59 |
+
skills_datas.append(skill)
|
60 |
+
return skills_datas
|
61 |
+
|
62 |
+
|
63 |
+
def time_ago(updated_at_str):
|
64 |
+
now = datetime.now()
|
65 |
+
updated_at = datetime.strptime(updated_at_str, '%Y-%m-%d %H:%M:%S')
|
66 |
+
delta = now - updated_at
|
67 |
+
|
68 |
+
minutes = delta.total_seconds() / 60
|
69 |
+
hours = minutes / 60
|
70 |
+
days = hours / 24
|
71 |
+
months = days / 30.44 # An average month length
|
72 |
+
years = days / 365.25 # Account for leap years
|
73 |
+
|
74 |
+
if minutes < 60:
|
75 |
+
return f"{int(minutes)} minutes ago"
|
76 |
+
elif hours < 24:
|
77 |
+
return f"{int(hours)} hours ago"
|
78 |
+
elif days < 30.44:
|
79 |
+
return f"{int(days)} days ago"
|
80 |
+
elif months < 12:
|
81 |
+
return f"{int(months)} months ago"
|
82 |
+
else:
|
83 |
+
return f"{int(years)} years ago"
|