簡體   English   中英

如何在 Streamlit 中設置布局大小?

[英]How Can I set layout size in Streamlit?

是否可以在 Streamlit 布局中設置高度和寬度? 我知道可以設置 layout='wide' 和 'centered' 但這對我來說還不夠。 Map 使用寬時太大,使用居中時太小。 當我設置尺寸葉 Map 時,布局和 map 之間有一個空白。 我想設置這個大小沒有空格。

寬: https://gyazo.com/0af46f5efc80ff079410a9aeae1d38b0居中: https://gyazo.com/22bac90498e13

Streamlit map 只是st.pydeck_chart的包裝。 這有助於輕松制作 map 圖表,但也有一些限制,例如設置高度和寬度。

如果您想控制呈現的 map 的高度和寬度,最好的選擇是使用具有高度和寬度參數的st.pydeck_chart

這是使用st.pydeck_chart創建 map 的代碼:

import streamlit as st
import pandas as pd
import numpy as np
import pydeck as pdk


height = 500
width = 500

df = pd.DataFrame(
    np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4],
    columns=['lat', 'lon'])

st.pydeck_chart(pdk.Deck(
     map_style='mapbox://styles/mapbox/light-v9',
     initial_view_state=pdk.ViewState(
         latitude=37.76,
         longitude=-122.4,
         zoom=11,
         height=height,
         width=width
     ),
     layers=[
         pdk.Layer(
             'ScatterplotLayer',
             data=df,
             get_position='[lon, lat]',
             get_color='[200, 30, 0, 160]',
             get_radius=100,
             auto_highlight=True
         ),
     ],
 ))

Output 高度=500,寬度=500:

輸出1

Output,高度=100,寬度=100:

輸出2

Note - st.map creates a scatter plot chart on top of a map but using st.pydeck_chart gives us more flexibility and options in map creation and design.

只需在 app.py 中調用此 function _max_width_(80)即可始終以寬模式啟動。 更多關於自定義渲染寬度

def _max_width_(prcnt_width:int = 75):
    max_width_str = f"max-width: {prcnt_width}%;"
    st.markdown(f""" 
                <style> 
                .reportview-container .main .block-container{{{max_width_str}}}
                </style>    
                """, 
                unsafe_allow_html=True,
    )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM