The issue you're encountering can be due to several reasons, such as issues with event handling or improper assignment of values to input components. Let's refine the on_ticker_select function and ensure that the inputs are correctly updated. We'll make sure that the values are being assigned and are compatible by casting them to strings where necessary.
Here’s a modified version focusing on these aspects:
from nicegui import ui
# Sample data containing ticker and other related information
derivative_data = [
{'ticker': 'AAPL', 'premium': 5.25, 'strike_price': 150, 'trade_direction': 'CALL', 'days_until_maturity': 30},
{'ticker': 'GOOGL', 'premium': 10.00, 'strike_price': 2800, 'trade_direction': 'PUT', 'days_until_maturity': 60},
{'ticker': 'MSFT', 'premium': 8.00, 'strike_price': 300, 'trade_direction': 'CALL', 'days_until_maturity': 45},
# Add more derivative records as needed
]
# Function to find derivative info based on ticker
def get_derivative_info(ticker):
for derivative in derivative_data:
if derivative['ticker'] == ticker:
return derivative
return None
# Function to handle auto-suggestion updates
def on_ticker_input_change(prefix):
suggestions = [d['ticker'] for d in derivative_data if prefix.upper() in d['ticker']]
return suggestions
# Main function to be called when a ticker is selected
def on_ticker_select(ticker):
derivative_info = get_derivative_info(ticker)
if derivative_info:
premium_input.set_value(str(derivative_info['premium']))
strike_price_input.set_value(str(derivative_info['strike_price']))
trade_direction_input.set_value(derivative_info['trade_direction'])
days_until_maturity_input.set_value(str(derivative_info['days_until_maturity']))
else:
# Clear the inputs if no data is found
premium_input.set_value('')
strike_price_input.set_value('')
trade_direction_input.set_value('')
days_until_maturity_input.set_value('')
# Create the UI
ui.label('Financial Derivatives Input Form').style('font-weight', 'bold', 'font-size', '20px')
ui.label('Ticker:')
ticker_input = ui.autocomplete(on_suggestions=on_ticker_input_change, on_select=on_ticker_select)
ui.label('Premium:')
premium_input = ui.input(readonly=True)
ui.label('Strike Price:')
strike_price_input = ui.input(readonly=True)
ui.label('Trade Direction:')
trade_direction_input = ui.input(readonly=True)
ui.label('Days Until Maturity:')
days_until_maturity_input = ui.input(readonly=True)
ui.run()
Key Adjustments:
-
Use set_value Method: The code now uses set_value to update the input values, which ensures that the assignment operation properly updates the UI component's state.
-
Event Binding: Make sure that on_suggestions and on_select are tied to appropriate functions. The on_suggestions provides a dynamic list of entries for each input update, while on_select modifies the values based on the selected ticker.
-
String Conversion: Convert numbers to strings using str() to correctly display them in input fields. This ensures data compatibility with text-based input components.
These adjustments should help resolve the issue where values don't seem to update upon selecting a ticker. If the suggestions are processed properly, and all data matches an expected format, the selected ticker should populate all related fields correctly.