Pin / app.py
Alikhani099961's picture
Update app.py
3bb6b7c verified
import streamlit as st
from pine_parser import PineScriptParser
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configure page
st.set_page_config(
page_title="Pine Script Analyzer Pro",
page_icon="🧠",
layout="wide"
)
# Custom CSS for better UI
st.markdown("""
<style>
.code-container {
border-radius: 0.5rem;
padding: 1rem;
background-color: #f8f9fa;
border: 1px solid #dee2e6;
}
.fixed-code {
background-color: #e8f5e9;
}
.error-message {
color: #d32f2f;
font-weight: bold;
}
.warning-message {
color: #ffa000;
}
</style>
""", unsafe_allow_html=True)
def main():
st.title("🧠 Pine Script Analyzer Pro")
st.caption("Professional-grade analysis and optimization for TradingView Pine Script")
with st.expander("πŸ“ Paste Your Pine Script Code", expanded=True):
user_code = st.text_area(
"Paste your Pine Script code here:",
height=300,
placeholder="""//@version=5
indicator("My Indicator")
plot(close)""",
label_visibility="collapsed"
)
if st.button("πŸ” Analyze & Optimize Code", use_container_width=True):
if not user_code.strip():
st.error("Please enter Pine Script code to analyze")
else:
with st.spinner("Analyzing code with professional-grade tools..."):
try:
parser = PineScriptParser(user_code)
results = {
"version": parser.detect_version() or "Unknown",
"errors": parser.find_errors(),
"warnings": parser.find_warnings(),
"optimizations": parser.find_optimizations(),
"fixed_code": parser.generate_fixed_code()
}
display_results(results)
except Exception as e:
st.error(f"Professional analysis failed: {str(e)}")
def display_results(results):
"""Display analysis results in professional UI"""
# Version and summary
col1, col2 = st.columns(2)
with col1:
st.metric("Detected Version", results["version"])
with col2:
st.metric("Issues Found", f"{len(results['errors'])} Errors, {len(results['warnings'])} Warnings")
# Tabs for different analysis aspects
tab1, tab2, tab3, tab4 = st.tabs(["🚨 Errors", "⚠️ Warnings", "✨ Optimizations", "βœ… Fixed Code"])
with tab1:
if results["errors"]:
for error in results["errors"]:
st.markdown(f'<p class="error-message">β€’ {error}</p>', unsafe_allow_html=True)
else:
st.success("No critical errors found!")
with tab2:
if results["warnings"]:
for warning in results["warnings"]:
st.markdown(f'<p class="warning-message">β€’ {warning}</p>', unsafe_allow_html=True)
else:
st.success("No warnings detected!")
with tab3:
if results["optimizations"]:
for opt in results["optimizations"]:
st.info(f"β€’ {opt}")
else:
st.success("Code is already optimized!")
with tab4:
st.markdown('<div class="code-container fixed-code">', unsafe_allow_html=True)
st.code(results["fixed_code"], language="javascript")
st.markdown('</div>', unsafe_allow_html=True)
st.download_button(
"πŸ“‹ Download Optimized Code",
data=results["fixed_code"],
file_name="optimized_script.pine",
mime="text/plain",
use_container_width=True
)
if __name__ == "__main__":
main()