Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from pine_parser import PineScriptParser
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# Configure page
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Pine Script Analyzer Pro",
|
| 12 |
+
page_icon="π§ ",
|
| 13 |
+
layout="wide"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Custom CSS for better UI
|
| 17 |
+
st.markdown("""
|
| 18 |
+
<style>
|
| 19 |
+
.code-container {
|
| 20 |
+
border-radius: 0.5rem;
|
| 21 |
+
padding: 1rem;
|
| 22 |
+
background-color: #f8f9fa;
|
| 23 |
+
border: 1px solid #dee2e6;
|
| 24 |
+
}
|
| 25 |
+
.fixed-code {
|
| 26 |
+
background-color: #e8f5e9;
|
| 27 |
+
}
|
| 28 |
+
.error-message {
|
| 29 |
+
color: #d32f2f;
|
| 30 |
+
font-weight: bold;
|
| 31 |
+
}
|
| 32 |
+
.warning-message {
|
| 33 |
+
color: #ffa000;
|
| 34 |
+
}
|
| 35 |
+
</style>
|
| 36 |
+
""", unsafe_allow_html=True)
|
| 37 |
+
|
| 38 |
+
def main():
|
| 39 |
+
st.title("π§ Pine Script Analyzer Pro")
|
| 40 |
+
st.caption("Professional-grade analysis and optimization for TradingView Pine Script")
|
| 41 |
+
|
| 42 |
+
with st.expander("π Paste Your Pine Script Code", expanded=True):
|
| 43 |
+
user_code = st.text_area(
|
| 44 |
+
"Paste your Pine Script code here:",
|
| 45 |
+
height=300,
|
| 46 |
+
placeholder="""//@version=5
|
| 47 |
+
indicator("My Indicator")
|
| 48 |
+
plot(close)""",
|
| 49 |
+
label_visibility="collapsed"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
if st.button("π Analyze & Optimize Code", use_container_width=True):
|
| 53 |
+
if not user_code.strip():
|
| 54 |
+
st.error("Please enter Pine Script code to analyze")
|
| 55 |
+
else:
|
| 56 |
+
with st.spinner("Analyzing code with professional-grade tools..."):
|
| 57 |
+
try:
|
| 58 |
+
parser = PineScriptParser(user_code)
|
| 59 |
+
results = {
|
| 60 |
+
"version": parser.detect_version() or "Unknown",
|
| 61 |
+
"errors": parser.find_errors(),
|
| 62 |
+
"warnings": parser.find_warnings(),
|
| 63 |
+
"optimizations": parser.find_optimizations(),
|
| 64 |
+
"fixed_code": parser.generate_fixed_code()
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
display_results(results)
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
st.error(f"Professional analysis failed: {str(e)}")
|
| 71 |
+
|
| 72 |
+
def display_results(results):
|
| 73 |
+
"""Display analysis results in professional UI"""
|
| 74 |
+
|
| 75 |
+
# Version and summary
|
| 76 |
+
col1, col2 = st.columns(2)
|
| 77 |
+
with col1:
|
| 78 |
+
st.metric("Detected Version", results["version"])
|
| 79 |
+
with col2:
|
| 80 |
+
st.metric("Issues Found", f"{len(results['errors'])} Errors, {len(results['warnings'])} Warnings")
|
| 81 |
+
|
| 82 |
+
# Tabs for different analysis aspects
|
| 83 |
+
tab1, tab2, tab3, tab4 = st.tabs(["π¨ Errors", "β οΈ Warnings", "β¨ Optimizations", "β
Fixed Code"])
|
| 84 |
+
|
| 85 |
+
with tab1:
|
| 86 |
+
if results["errors"]:
|
| 87 |
+
for error in results["errors"]:
|
| 88 |
+
st.markdown(f'<p class="error-message">β’ {error}</p>', unsafe_allow_html=True)
|
| 89 |
+
else:
|
| 90 |
+
st.success("No critical errors found!")
|
| 91 |
+
|
| 92 |
+
with tab2:
|
| 93 |
+
if results["warnings"]:
|
| 94 |
+
for warning in results["warnings"]:
|
| 95 |
+
st.markdown(f'<p class="warning-message">β’ {warning}</p>', unsafe_allow_html=True)
|
| 96 |
+
else:
|
| 97 |
+
st.success("No warnings detected!")
|
| 98 |
+
|
| 99 |
+
with tab3:
|
| 100 |
+
if results["optimizations"]:
|
| 101 |
+
for opt in results["optimizations"]:
|
| 102 |
+
st.info(f"β’ {opt}")
|
| 103 |
+
else:
|
| 104 |
+
st.success("Code is already optimized!")
|
| 105 |
+
|
| 106 |
+
with tab4:
|
| 107 |
+
st.markdown('<div class="code-container fixed-code">', unsafe_allow_html=True)
|
| 108 |
+
st.code(results["fixed_code"], language="javascript")
|
| 109 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 110 |
+
|
| 111 |
+
st.download_button(
|
| 112 |
+
"π Download Optimized Code",
|
| 113 |
+
data=results["fixed_code"],
|
| 114 |
+
file_name="optimized_script.pine",
|
| 115 |
+
mime="text/plain",
|
| 116 |
+
use_container_width=True
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
if __name__ == "__main__":
|
| 120 |
+
main()
|