Fixes the issue #8, Add Profitability Forecast Charts Using Plotly #19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Title: Fixes the issue #8 of adding insightful and decision making profitability forecast charts using plotly
Description:
This pull request introduces a new crop economics visualization module to the KrishiMitra Streamlit app, enabling farmers to compare profitability across crops using interactive Plotly bar charts. Additionally, it updates the crop_economics.json file by adding 10 new crops to enhance the dataset for economic analysis. The changes aim to provide farmers with actionable insights into crop profitability, factoring in realistic cost and yield variations.
Changes Made:
New Crop Economics Visualization Module:
Added a new section titled "Crop Economics Analysis" to the Streamlit app, placed after the "Mandi Prices" section.
Implemented functionality to load crop data from crop_economics.json using a load_crop_data function with robust error handling.
Enhanced economic calculations in the calculate_economics function to include:
Input costs: Seed, fertilizer, labor, irrigation, and pest control.
Yield variability (±10%) to simulate real-world fluctuations.
Market price fluctuation (±5%) to account for market volatility.
Profit margin calculation (Profit/Income * 100).
Added interactive visualizations using Plotly:
A single bar chart comparing profits across crops (using plotly.express) .
An optional grouped bar chart (toggled via checkbox) comparing input cost, income, and profit (using plotly.graph_objects) for detailed decision making skills for the selection of crops based on profit might be obtained in the future by performing various complex calculations with accurate results for the benefit of farmers.
Text labels on bars for exact values and a clean white theme for readability.
Displayed additional metrics: highest profit crop and highest profit margin crop.
Added new dependencies: plotly, json, and random to the imports.
Updated crop_economics.json:
Expanded the dataset from 5 crops (wheat, rice, maize, potato, mustard) to 15 crops by adding:
Sugarcane, tomato, soybean, cotton, barley, chana (chickpea), groundnut, bajra (pearl millet), moong dal (green gram), and onion.
Ensured all crops include required fields: seed_cost, fertilizer, labor_cost, irrigation_cost, pest_control_cost, yield, and market_price.
Values are based on typical Indian agricultural data (costs in ₹/hectare, yields in quintals/hectare, market prices in ₹/quintal, except sugarcane yield which is higher).
Code Integration:
Integrated the new module into the existing Streamlit app without modifying other sections (chatbot, fertilizer recommendation, loan/subsidy checker, etc.).
Ensured the module dynamically handles any number of crops in the JSON file.
Purpose:
The crop economics visualization module helps farmers make informed decisions by comparing profitability across 15 crops, accounting for realistic factors like irrigation, pest control, yield variability, and market price fluctuations. The expanded dataset ensures a broader range of crops relevant to Indian farmers, enhancing the app's utility.
Testing Instructions
Setup:
Ensure dependencies are installed: pip install streamlit pandas gtts openai plotly.
Place the updated crop_economics.json in the same directory as the Streamlit script.
Update the background image path in set_bg_local to a valid local image file as of now i have downloaded image from the google and updated the path. You can replace it with the your file path.
Run the App:
Execute: streamlit run farmer.py.
Verify Changes:
Navigate to the "Crop Economics Analysis" section in the app.
Confirm the data table displays all 15 crops with columns: Crop, Input Cost, Income, Profit, Profit Margin (%), Adjusted Yield, Adjusted Price.
Check the "Profit Comparison Across Crops" bar chart shows profits for all 15 crops with correct labels (e.g., ₹ values).
Enable the "Show Grouped Bar Chart" checkbox and verify the grouped bar chart compares input cost, income, and profit for all crops.
Validate the "Additional Metrics" section correctly identifies the highest profit and profit margin crops.
Ensure no errors occur when loading crop_economics.json (test with a missing file to verify error handling).
Confirm existing app features (chatbot, weather alerts, etc.) remain unaffected.
Related Issues
None (this is a new feature request).
Checklist
Code follows project style guidelines.
Tested locally with all 15 crops displayed correctly.
Visualizations render without errors in Streamlit.
JSON file is valid and correctly formatted.
No impact on existing functionality.
Ready for review.
Screenshots:




Insights: In the visualizations i had implemented enhances the decision making skills in farmers of selecting the crop that yields more profit by considering all the parameters of the input costs and also provided for each visualization might be in table format or via bar charts, one can easily understand the situation and provided with the functionality to each visualization such that they can download the visualizations. This helps the farmers a lot who are of unable gain the insights textually and they can easily understand the profitability of each crop. This feature enhances this application making a robust application.
I hope this solution resolves this issue and could you please mention the level as 2 or 3 for this issue as a part of GSSOC 25. Thank you once again for trusting me and providing me an opportunity for contributing myself to the development of farmers