Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i have a python app, but i want the plots to be more orgranized. I want to move the plots to cover more of the

i have a python app, but i want the plots to be more orgranized. I want to move the plots to cover more of the window and have more space for other plot and reports, any help is really appreciated please:
import streamlit as st
from sqlalchemy import create_engine
import pandas as pd
import plotly.express as px
# Function to create a database connection using SQLAlchemy
def create_db_connection():
db_url ="mysql+mysqlconnector://root:peter@localhost/main_items"
engine = create_engine(db_url)
return engine
# --- Fetch data from the database ---
def load_data():
engine = create_db_connection()
query = "SELECT * FROM items_table"
data = pd.read_sql(query, engine)
return data
# --- plot the data in a scatter plot ---
def plot_scatter_chart(data):
# Convert 'Bundles_Boxes' to numeric, setting errors to 'NaN'
data['Bundles_Boxes']= pd.to_numeric(data['Bundles_Boxes'], errors='coerce')
# Replace NaN values with a default size, e.g.,0 or the mean size
data['Bundles_Boxes'].fillna(0, inplace=True)
# Calculate total and percentage
total_bundles = data['Bundles_Boxes'].sum()
data['Percentage']=(data['Bundles_Boxes']/ total_bundles)*100
# Create the scatter plot
fig = px.scatter(
data_frame=data,
x='Description',
y='Percentage',
size='Bundles_Boxes', # The size argument represents the size of each dot.
hover_data=['Bundles_Boxes'], # Display bundles/boxes info on hover
labels={'Description': 'Item Description', 'Percentage': 'Percentage of Total Inventory'},
title='Percentage of Inventory by Item'
)
# Update layout to adjust margins
fig.update_layout(
margin=dict(l=20, r=20, t=20, b=20), # 'l' adjusts the left margin
xaxis_tickangle=-45
)
return fig
# --- plot data in a pie chart to display total inventory usage per Bundles/Boxes ---
def plot_pie_chart(data):
# Calculate total bundles for percentage calculation
total_bundles = data['Bundles_Boxes'].sum()
# Add a percentage column to the dataframe
data['Inventory_Percentage']=(data['Bundles_Boxes']/ total_bundles)*100
# Create a pie chart with Plotly
fig = px.pie(
data_frame=data,
names='Description',
values='Inventory_Percentage',
title='Percentage of Inventory by Bundles/Boxes per Item'
)
return fig
# --- plot data into a bar chart ---
def plot_bar_chart(data):
fig = px.bar(data_frame=data, x='Description', y='Units_Pieces_Each', title='Item Description')
fig.update_layout(xaxis_tickangle=-45)
return fig
# Define the Streamlit page
def main():
st.title("Inventory Dashboard")
# Load the data
data = load_data().copy()
# Define the layout for charts
col1, col2= st.columns([5,6]) # Adjust the ratio based on your requirement
with col1:
# Plotting the scatter chart
scatter_chart = plot_scatter_chart(data)
st.plotly_chart(scatter_chart, use_container_width=True)
with col2:
# Plotting the pie chart
pie_chart = plot_pie_chart(data)
st.plotly_chart(pie_chart, use_container_width=True)
# Plotting the bar chart below the scatter and pie charts
st.write('Item Description')
bar_chart = plot_bar_chart(data)
st.plotly_chart(bar_chart, use_container_width=True)
# Run the main function when the script is executed
if __name__=="__main__":
main()
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions

Question

3. How might we use our elected officials?

Answered: 1 week ago

Question

How does this scenario illustrate the process of mainstreaming?

Answered: 1 week ago

Question

What are personal and social media?

Answered: 1 week ago