import gradio as gr import altair as alt import pandas as pd import os def create_portfolio_graph(amount): # Calculate positions based on the provided amount long_position = float(os.getenv('LONG_PORTION')) * amount short_position = float(os.getenv('SHORT_PORTION')) * amount # Data for the bars positions = ['Long Position', 'Short Position'] percentages = [long_position, short_position] data = pd.DataFrame({'Position': positions, 'Percentage': percentages}) # Creating the bar plot with adjusted width and colors chart = alt.Chart(data).mark_bar().encode( x=alt.X('Position:N', axis=alt.Axis(title=None)), y=alt.Y('Percentage:Q', axis=alt.Axis(title='Funds')), color=alt.Color('Position:N', scale=alt.Scale(range=['blue', 'red'])) ).properties(width=400) # Adjust width as needed return chart with gr.Blocks() as demo: title = gr.Markdown( """ # Insert Portfolio Value and Click Submit to See the Recommended Portfolio Allocation """ ) amount = gr.Number(value=0, label="Type in the total amount of funds in your portfolio including leverage") button = gr.Button(value="Submit") plot = gr.Plot(label="Portfolio Composition Chart") demo.load(create_portfolio_graph, inputs=[amount], outputs=[plot]) button.click(create_portfolio_graph, [amount], plot) if __name__ == "__main__": demo.launch()