#!/usr/bin/env python # -*- coding: utf-8 -*- # @Project : Python. # @File : 666_😝_TEST # @Time : 2023/3/9 上午10:28 # @Author : yuanjie # @WeChat : meutils # @Software : PyCharm # @Description : https://github.com/streamlit/example-app-commenting/blob/main/streamlit_app.py """ 不断刷新数据图表 https://blog.csdn.net/qq_42761569/article/details/123418493 http://cw.hubwiz.com/card/c/streamlit-manual/1/6/13/ """ import altair as alt def get_chart(data): # 鼠标悬停 hover = alt.selection_single( fields=["date"], nearest=True, on="mouseover", empty="none", ) lines = ( alt.Chart(data, title="Evolution of stock prices") .mark_line() .encode( x="date", y="price", color="symbol", strokeDash="symbol", ) ) # Draw points on the line, and highlight based on selection points = lines.transform_filter(hover).mark_circle(size=65) # Draw a rule at the location of the selection tooltips = ( alt.Chart(data) .mark_rule() .encode( x="date", y="price", opacity=alt.condition(hover, alt.value(0.3), alt.value(0)), tooltip=[ alt.Tooltip("date", title="Date"), alt.Tooltip("price", title="Price (USD)"), ], ) .add_selection(hover) ) return (lines + points + tooltips).interactive() # source = data.stocks() # all_symbols = source.symbol.unique() # symbols = st.multiselect("Choose stocks to visualize", all_symbols, all_symbols[:3]) # # # source = source[source.symbol.isin(symbols)] # chart = get_chart(source) # st.altair_chart(chart, use_container_width=True)