From 506d983d9b852695d709f50db1813e38d3de6830 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 21 Jun 2025 00:07:00 +0800 Subject: [PATCH] Sat Jun 21 00:07:00 CST 2025 inscode --- fun_facts.csv | 41 +++++++++++++++ main.py | 126 ++++++++++++++++++++++++++++++++++++++++++++++- requirements.txt | 54 ++++++++++++++++++++ 3 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 fun_facts.csv diff --git a/fun_facts.csv b/fun_facts.csv new file mode 100644 index 0000000..75f9070 --- /dev/null +++ b/fun_facts.csv @@ -0,0 +1,41 @@ +Fact,Category,Link +"地球上有超过 870 万种生物,但只有约 120 万种已被识别。",科学,https://zh.wikipedia.org/wiki/%E7%94%9F%E7%89%A9%E5%A4%9A%E6%A0%B7%E6%80%A7 +"蜜蜂是唯一能生产人类可食用食物的昆虫。",动物,https://zh.wikipedia.org/wiki/%E8%9C%9C%E8%9C%82 +"世界上最长的山脉是安第斯山脉,长约 7000 公里。",地理,https://zh.wikipedia.org/wiki/%E5%AE%89%E7%AC%9B%E6%96%AF%E5%B1%B1%E8%84%89 +"北极熊的皮肤是黑色的,毛发是透明的。",动物,https://zh.wikipedia.org/wiki/%E5%8C%97%E6%9E%81%E7%86%8A +"人类打喷嚏时,眼睛是无法睁开的。",人体,https://zh.wikipedia.org/wiki/%E5%97%85%E5%9A%8F +"宇宙的味道被描述为类似于烤牛排、燃烧的金属和焊烟的味道。",科学,https://zh.wikipedia.org/wiki/%E5%AE%87%E5%AE%99%E8%87%AD%E6%B0%94 +"一只鸡一生中大约能产下 200 到 250 个蛋。",动物,https://zh.wikipedia.org/wiki/%E9%B8%A1 +"金字塔的建造时间比第一个互联网浏览器的出现时间更接近现代。",历史,https://zh.wikipedia.org/wiki/%E9%87%91%E5%AD%97%E5%A4%B7 +"如果你把地球上的所有蚂蚁都加起来,它们的总重量将超过所有人类的总重量。",动物,https://zh.wikipedia.org/wiki/%E8%9A%82%E8%9A%81 +"月球每年以大约 3.8 厘米的速度远离地球。",科学,https://zh.wikipedia.org/wiki/%E6%9C%88%E7%90%83 +"人一生中会产生足够多的唾液来填满两个游泳池。",人体,https://zh.wikipedia.org/wiki/%E5%94%BE%E6%B6%B2 +"章鱼有三个心脏,两个将血液泵到鳃,一个泵到身体其他部分。",动物,https://zh.wikipedia.org/wiki/%E7%AB%A0%E9%B1%BC +"香蕉是一种浆果,而草莓却不是。",食物,https://zh.wikipedia.org/wiki/%E9%A6%99%E8%95%89 +"世界上最长的单词是 'Pneumonoultramicroscopicsilicovolcanoconiosis',指一种肺病。",语言,https://zh.wikipedia.org/wiki/Pneumonoultramicroscopicsilicovolcanoconiosis +"如果你对着一个杯子唱歌,它的玻璃可能会碎裂,这被称为共振。",科学,https://zh.wikipedia.org/wiki/%E5%85%B1%E6%8C%AF +"可口可乐最初是绿色的。",历史,https://zh.wikipedia.org/wiki/%E5%8F%AF%E5%8F%A3%E5%8F%AF%E4%B9%90 +"蒙娜丽莎的眉毛是画师特意没有画上去的。",艺术,https://zh.wikipedia.org/wiki/%E8%92%99%E5%A8%9C%E4%B8%BD%E8%8E%8E +"如果你在太空中哭泣,眼泪会粘在你的脸上,因为没有重力。",科学,https://zh.wikipedia.org/wiki/%E5%A4%B1%E9%87%8D +"鸵鸟的眼睛比它的大脑还要大。",动物,https://zh.wikipedia.org/wiki/%E9%B9%9D%E9%B8%9F +"世界上大约有 10% 的人是左撇子。",人体,https://zh.wikipedia.org/wiki/%E5%B7%A6%E6%92%B4%E5%AD%90 +"人类无法在睡梦中闻到任何东西。",人体,https://zh.wikipedia.org/wiki/%E7%9D%A1%E7%9C%A0 +"你的鼻子和耳朵在你的一生中都在持续生长。",人体,https://zh.wikipedia.org/wiki/%E9%BC%BB%E5%AD%90 +"海豚睡觉时会睁着一只眼睛。",动物,https://zh.wikipedia.org/wiki/%E6%B5%B7%E8%B1%9A +"鳄鱼不能吐舌头。",动物,https://zh.wikipedia.org/wiki/%E9%B3%84%E9%B1%BC +"一部完整的《圣经》在太空中被翻译。",历史,https://zh.wikipedia.org/wiki/%E8%8B%B1%E5%9B%BD%E5%9C%A3%E7%BB%8F%E4%BC%9A +"世界上最古老的口香糖已有 9000 年的历史。",历史,https://zh.wikipedia.org/wiki/%E5%8F%A3%E9%A6%99%E7%B3%96 +"蜂鸟是唯一可以向后飞的鸟类。",动物,https://zh.wikipedia.org/wiki/%E8%9C%9C%E8%9C%82 +"人类的 DNA 与香蕉的 DNA 有大约 50% 的相似性。",科学,https://zh.wikipedia.org/wiki/%E8%84%B1%E6%B0%A7%E6%A0%B8%E7%B3%96%E6%A0%B8%E9%85%B8 +"地球上每天大约有 100 吨的宇宙尘埃落到地表。",科学,https://zh.wikipedia.org/wiki/%E5%AE%87%E5%AE%99%E5%B0%98%E5%9F%83 +"在土星和木星上会下钻石雨。",科学,https://zh.wikipedia.org/wiki/%E5%9C%9F%E6%98%9F +"如果太阳突然消失,地球需要 8 分 20 秒才能感受到它的影响。",科学,https://zh.wikipedia.org/wiki/%E5%85%89%E9%80%9F +"世界上海拔最高的厕所位于珠穆朗玛峰,海拔约 8700 米。",地理,https://zh.wikipedia.org/wiki/%E7%8F%A0%E7%A9%86%E6%9C%97%E7%8E%9B%E5%B3%B0 +"树懒每周只排便一次,而且通常是在地面上。",动物,https://zh.wikipedia.org/wiki/%E6%A0%91%E6%87%92 +"人的心脏每年平均跳动 3500 万次。",人体,https://zh.wikipedia.org/wiki/%E5%BF%83%E8%84%8F +"猫的尿液在紫外线下是会发光的。",动物,https://zh.wikipedia.org/wiki/%E7%8C%AB +"大象是唯一不会跳跃的哺乳动物。",动物,https://zh.wikipedia.org/wiki/%E5%A4%A7%E8%B1%A1 +"一个正常的云朵大约重 55 万公斤。",科学,https://zh.wikipedia.org/wiki/%E4%BA%91 +"世界上最老的已知活生物是一棵名为 'Methuselah' 的狐尾松,大约有 4850 岁。",植物,https://zh.wikipedia.org/wiki/%E7%8B%90%E5%B0%BE%E6%9D%BE +"太空中没有声音,因为没有介质传播声波。",科学,https://zh.wikipedia.org/wiki/%E5%A4%AA%E7%A9%BA +"长颈鹿的舌头是蓝色的,长达 45 厘米。",动物,https://zh.wikipedia.org/wiki/%E9%95%BF%E9%A2%88%E9%B9%BF \ No newline at end of file diff --git a/main.py b/main.py index 4c0c135..18b48d8 100644 --- a/main.py +++ b/main.py @@ -1 +1,125 @@ -print('欢迎来到 InsCode') \ No newline at end of file +import streamlit as st +import pandas as pd +import random +import time # 用于模拟加载时间 + +# --- 页面配置(放在最前面)--- +st.set_page_config( + page_title="趣闻发现器✨", + page_icon="💡", + layout="centered", # 可以是 "centered" 或 "wide" + initial_sidebar_state="expanded" # 侧边栏初始状态 +) + +# --- 侧边栏内容 --- +with st.sidebar: + st.title("关于这个应用") + st.info( + "欢迎来到**趣闻发现器**!\n\n" + "这个应用旨在通过随机展示有趣的小知识,\n" + "让你在忙碌之余也能轻松获取新知,\n" + "发现世界的奇妙之处。✨" + ) + st.write("---") + st.markdown("Developed with ❤️ using [Streamlit](https://streamlit.io/)") + st.markdown("数据来源:人工整理") + st.write("---") + # 添加一个小的提示,增加趣味性 + st.caption("小提示:每次点击 '再来一个!🚀' 都能带来惊喜哦!") + +# --- 主页面标题和描述 --- +st.title("🌍 趣闻发现器") +st.markdown("### 💡 每次点击,发现一个新奇世界!") +st.write("点击下面的按钮,探索一条随机的趣味知识。") +st.write("---") + +# --- 数据加载 --- +@st.cache_data +def load_data(): + """ + 加载趣闻数据。 + 使用 st.cache_data 缓存数据,避免每次运行都重新加载。 + """ + try: + df = pd.read_csv("fun_facts.csv") + # 确保 'Category' 和 'Link' 列存在,如果不存在则添加并填充NaN + if 'Category' not in df.columns: + df['Category'] = pd.NA + if 'Link' not in df.columns: + df['Link'] = pd.NA + return df + except FileNotFoundError: + st.error("错误:找不到 'fun_facts.csv' 文件。请确保文件与 'fun_fact_app.py' 在同一目录下。") + st.stop() + except Exception as e: + st.error(f"加载数据时发生错误:{e}") + st.stop() + +facts_df = load_data() + +# --- 筛选器:如果数据不为空且包含类别列 --- +if not facts_df.empty and 'Category' in facts_df.columns: + all_categories = ['所有类别'] + sorted(facts_df['Category'].dropna().unique().tolist()) + selected_category = st.selectbox( + "按类别筛选趣闻:", + options=all_categories, + index=0, + help="选择一个类别来获取特定类型的趣闻" + ) + + if selected_category == '所有类别': + filtered_df = facts_df + else: + filtered_df = facts_df[facts_df['Category'] == selected_category] + if filtered_df.empty: + st.warning(f"在 '{selected_category}' 类别下没有找到趣闻。请尝试其他类别或 '所有类别'。") + filtered_df = facts_df # 如果筛选结果为空,则回到所有数据,避免错误 +else: + filtered_df = facts_df # 如果没有类别列或数据为空,则使用全部数据 + +# --- 核心功能:随机展示趣闻 --- +if not filtered_df.empty: + # 使用 Streamlit 的 session state 来保存当前显示的趣闻索引 + if 'current_fact_index' not in st.session_state: + st.session_state.current_fact_index = random.randint(0, len(filtered_df) - 1) + + # 如果筛选后的数据发生变化,重置索引 + if 'last_filtered_category' not in st.session_state or \ + st.session_state.last_filtered_category != selected_category: + st.session_state.current_fact_index = random.randint(0, len(filtered_df) - 1) + st.session_state.last_filtered_category = selected_category + + def get_random_fact(): + """获取一条随机趣闻并更新 session state""" + st.session_state.current_fact_index = random.randint(0, len(filtered_df) - 1) + # 模拟加载,给用户更好的体验 + with st.spinner("正在努力寻找新趣闻..."): + time.sleep(0.5) # 短暂暂停,模拟数据加载过程 + + # 使用列布局美化显示 + col1, col2 = st.columns([3, 1]) # 趣闻占3份宽度,按钮占1份 + + with col1: + st.markdown("### 🤩 精彩趣闻:") + current_fact_data = filtered_df.iloc[st.session_state.current_fact_index] + + # 使用 st.success 或 st.warning 来突出显示趣闻,增加视觉效果 + st.success(current_fact_data['Fact']) + + # 显示类别和链接 + st.markdown(f"📚 **类别**: {current_fact_data['Category'] if pd.notna(current_fact_data['Category']) else '未分类'}", unsafe_allow_html=True) + if pd.notna(current_fact_data['Link']): + st.markdown(f"👉 [**了解更多**]({current_fact_data['Link']})", unsafe_allow_html=True) + + with col2: + st.markdown("---") # 占位符,让按钮对齐 + # 使用 Streamlit 的 `use_container_width=True` 让按钮自适应列宽 + st.button("再来一个!🚀", on_click=get_random_fact, use_container_width=True) + +else: + st.warning("根据当前筛选条件,没有找到任何趣闻。请调整筛选器或检查数据。") + +# --- 页脚 --- +st.markdown("---") +st.markdown("感谢使用!希望你今天学到了一些有趣的东西。😊") +st.markdown("项目地址:[你的GitHub地址/Inscode地址] (可选,可删除)", unsafe_allow_html=True) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index e69de29..e7904d5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1,54 @@ +altair==5.5.0 +attrs==25.3.0 +blinker==1.9.0 +cachetools==6.1.0 +certifi==2025.6.15 +charset-normalizer==3.4.2 +click==8.1.8 +contourpy==1.3.0 +cycler==0.12.1 +Flask==3.1.1 +fonttools==4.58.0 +gitdb==4.0.12 +GitPython==3.1.44 +graphviz==0.20.3 +idna==3.10 +importlib_metadata==8.7.0 +importlib_resources==6.5.2 +itsdangerous==2.2.0 +Jinja2==3.1.6 +jsonschema==4.24.0 +jsonschema-specifications==2025.4.1 +kiwisolver==1.4.7 +Markdown==3.8 +MarkupSafe==3.0.2 +matplotlib==3.9.4 +narwhals==1.43.1 +natsort==8.4.0 +networkx==3.2.1 +numpy==2.0.2 +packaging==25.0 +pandas==2.3.0 +pillow==11.2.1 +protobuf==6.31.1 +pyarrow==20.0.0 +pydeck==0.9.1 +pymdown-extensions==10.15 +pyparsing==3.2.3 +python-dateutil==2.9.0.post0 +pytz==2025.2 +PyYAML==6.0.2 +referencing==0.36.2 +requests==2.32.4 +rpds-py==0.25.1 +six==1.17.0 +smmap==5.0.2 +streamlit==1.46.0 +tenacity==9.1.2 +toml==0.10.2 +tornado==6.5.1 +typing_extensions==4.14.0 +tzdata==2025.2 +urllib3==2.5.0 +Werkzeug==3.1.3 +zipp==3.22.0 -- GitLab