code-translator / app.py
Kvikontent's picture
Update app.py
82c6778 verified
raw
history blame contribute delete
No virus
1.9 kB
import streamlit as st
st.title("Code Converter")
st.divider()
input, output = st.columns(2)
out = ""
def python_to_javascript(python_code):
js_code = python_code
replacements = {
"print": "console.log",
"if": "if",
"elif": "else if",
"else": "else",
"True": "true",
"False": "false",
"==": "===",
":": "{"
"and": "&&",
"or": "||",
"not": "!",
"in": "includes",
"range": "Array.from(Array",
"for": "for",
"while": "while",
"continue": "continue",
"break": "break",
"def": "function",
"return": "return",
"lambda": "() =>",
"is": "===",
"None": "null",
"import": "import",
"from": "from",
"as": "as",
"class": "class",
"self": "this",
"del": "delete",
"try": "try",
"except": "catch",
"finally": "finally",
"raise": "throw",
"assert": "assert",
"async": "async",
"await": "await",
"yield": "yield",
"global": "global",
"nonlocal": "nonlocal",
"pass": "pass",
"with": "with",
"yield from": "yield from",
"async for": "for await",
"async with": "async with",
"async def": "async function"
}
for key, value in replacements.items():
if key in js_code:
js_code = js_code.replace(key, value)
return js_code
input_code_type = input.selectbox(
'Convert from',
("Python", "Coming soon")
)
output_code_type = output.selectbox(
'Convert to',
("JavaScript", "Comnig soon")
)
input_code = input.text_area("Enter yor code", height=300)
submit = input.button("Convert")
if submit:
out = python_to_javascript(input_code)
output_code = output.code(out, language=output_code_type, line_numbers=True)