File size: 1,689 Bytes
8234fd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import streamlit as st
from streamlit_ace import st_ace

_app_title = 'Streamlit Playground'

def security_check(code):
    import re
    restricted_modules = ['os','sys','pathlib']
    for module in restricted_modules:
        if re.search("import.*" + module, code) or re.search("from.*" + module, code):
            raise ImportError(module)
    return True

if __name__ == '__main__':
    st.set_page_config(
        page_title=_app_title,
        layout='wide',
        page_icon='πŸš€')

    st.title(_app_title)

    if 'code' in st.session_state.keys():
        code = st.session_state['code']
        if st.button('Back to Editor πŸ“'):
            st.session_state['code_bk'] = code
            st.session_state.pop('code')
            st.experimental_rerun()

        if security_check(code):
            st.write('Running Code:')
            st.code(code)
            st.write('Output:')
            exec(code)
    else:
        with st.form(key='editor'):
            if 'code_bk' in st.session_state.keys():
                code = st_ace(
                    theme='dracula',
                    auto_update=True,
                    language='python',
                    height=600,
                    font_size=16,
                    value=st.session_state['code_bk'])
            else:
                code = st_ace(
                    theme='dracula',
                    auto_update=True,
                    language='python',
                    height=600,
                    font_size=18)
            if st.form_submit_button('Run πŸƒβ€β™‚οΈ') and security_check(code):
                st.session_state['code'] = code
                st.experimental_rerun()