|
| 1 | +import paramiko |
| 2 | +import streamlit as st |
| 3 | +import time |
| 4 | + |
| 5 | + |
| 6 | +def login(): |
| 7 | + def on_login(): |
| 8 | + try: |
| 9 | + ssh_client = create_ssh_connection(host, username, password) |
| 10 | + |
| 11 | + toast_placeholder = st.empty() |
| 12 | + toast_placeholder.success("Login successful!") |
| 13 | + ssh_client.close() |
| 14 | + time.sleep(2) |
| 15 | + toast_placeholder.empty() |
| 16 | + |
| 17 | + title_placeholder.empty() |
| 18 | + host_placeholder.empty() |
| 19 | + username_placeholder.empty() |
| 20 | + password_placeholder.empty() |
| 21 | + st.session_state.logged_in = True |
| 22 | + st.session_state.host = host |
| 23 | + st.session_state.username = username |
| 24 | + st.session_state.password = password |
| 25 | + st.session_state.ssh_client = ssh_client |
| 26 | + |
| 27 | + except Exception as e: |
| 28 | + st.error(f"Login failed: {e}") |
| 29 | + |
| 30 | + title_placeholder = st.empty() |
| 31 | + host_placeholder = st.empty() |
| 32 | + username_placeholder = st.empty() |
| 33 | + password_placeholder = st.empty() |
| 34 | + login_button_placeholder = st.empty() |
| 35 | + |
| 36 | + title_placeholder.title("Login Page") |
| 37 | + |
| 38 | + host = host_placeholder.text_input("Host") |
| 39 | + username = username_placeholder.text_input("Username") |
| 40 | + password = password_placeholder.text_input("Password", type="password") |
| 41 | + |
| 42 | + if host and username and password: |
| 43 | + print(host) |
| 44 | + print(username) |
| 45 | + print(password) |
| 46 | + if not login_button_placeholder.button("Login", on_click=on_login): |
| 47 | + st.error("Invalid hostname, username or password, . Please try again.") |
| 48 | + |
| 49 | + |
| 50 | +def create_ssh_connection(host, username, password): |
| 51 | + ssh_client = paramiko.SSHClient() |
| 52 | + ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 53 | + ssh_client.connect(hostname=host, username=username, password=password) |
| 54 | + return ssh_client |
| 55 | + |
| 56 | + |
| 57 | +def create_project_dir(ssh_client, folder_name): |
| 58 | + ssh_client.exec_command(f"mkdir -p WebApp/{folder_name}") |
| 59 | + ssh_client.exec_command(f"mkdir -p WebApp/{folder_name}/test-dir") |
| 60 | + ssh_client.exec_command(f"mkdir -p WebApp/{folder_name}/EXPORTS") |
| 61 | + ssh_client.exec_command( |
| 62 | + f'echo "Hello World" > WebApp/{folder_name}/hello-world.txt' |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def download_file(ssh_client, remote_path, local_path): |
| 67 | + sftp_client = ssh_client.open_sftp() |
| 68 | + sftp_client.get(remote_path, local_path) |
| 69 | + sftp_client.close() |
| 70 | + |
| 71 | + |
| 72 | +def create_zip_folder(ssh_client, folder_path, zip_file_path): |
| 73 | + # Execute the zip command remotely to create the zip file |
| 74 | + ssh_client.exec_command(f"zip -r {zip_file_path} {folder_path}") |
| 75 | + |
| 76 | + |
| 77 | +def project_init(): |
| 78 | + st.title("Project Init Page") |
| 79 | + st.write("Welcome to the Project Init Page! You can start project init here.") |
| 80 | + |
| 81 | + |
| 82 | +def blasting(host, username, password): |
| 83 | + if not st.button("Back", on_click=logout): |
| 84 | + st.title("Blasting Page") |
| 85 | + folder_name = st.text_input("Project Name") |
| 86 | + uploaded_file = st.file_uploader("Upload FAST5 file as a zip", type=["zip"]) |
| 87 | + |
| 88 | + project_status = st.empty() |
| 89 | + |
| 90 | + if st.button("Start Blasting"): |
| 91 | + try: |
| 92 | + project_status.info("Project initializing started...") |
| 93 | + ssh_client = create_ssh_connection(host, username, password) |
| 94 | + create_project_dir(ssh_client, folder_name) |
| 95 | + project_status.success( |
| 96 | + f"Project '{folder_name}' is initiated successfully!" |
| 97 | + ) |
| 98 | + time.sleep(1) |
| 99 | + project_status.empty() |
| 100 | + |
| 101 | + ssh_client.close() |
| 102 | + except Exception as e: |
| 103 | + project_status.error(f"An error occurred: {e}") |
| 104 | + time.sleep(3) |
| 105 | + project_status.empty() |
| 106 | + |
| 107 | + if uploaded_file is not None: |
| 108 | + project_status.empty() |
| 109 | + try: |
| 110 | + project_status.info("Uploading project files...") |
| 111 | + ssh_client = create_ssh_connection(host, username, password) |
| 112 | + with ssh_client.open_sftp() as sftp: |
| 113 | + with sftp.file( |
| 114 | + f"WebApp/{folder_name}/{uploaded_file.name}", "wb" |
| 115 | + ) as f: |
| 116 | + f.write(uploaded_file.getvalue()) |
| 117 | + project_status.success("Project file are uploaded successfully!") |
| 118 | + |
| 119 | + # blasting is started |
| 120 | + time.sleep(1) |
| 121 | + project_status.info("Blasting started...") |
| 122 | + time.sleep(2) |
| 123 | + project_status.success("Blasting Completed...") |
| 124 | + time.sleep(2) |
| 125 | + project_status.empty() |
| 126 | + |
| 127 | + # TODO: |
| 128 | + |
| 129 | + # create a zip file of whole final project |
| 130 | + create_zip_folder( |
| 131 | + ssh_client, |
| 132 | + f"WebApp/{folder_name}", |
| 133 | + f"WebApp/{folder_name}/EXPORTS/exports.zip", |
| 134 | + ) |
| 135 | + |
| 136 | + project_status.info("Completed project files are Downloading...") |
| 137 | + |
| 138 | + # Download the project export file from the remote server |
| 139 | + download_file( |
| 140 | + ssh_client, |
| 141 | + f"WebApp/{folder_name}/EXPORTS/exports.zip", |
| 142 | + "exports.zip", |
| 143 | + ) |
| 144 | + |
| 145 | + project_status.success( |
| 146 | + "Completed project files are Downloaded successfully!" |
| 147 | + ) |
| 148 | + |
| 149 | + except Exception as e: |
| 150 | + project_status.error(f"An error occurred while uploading file: {e}") |
| 151 | + ssh_client.close() |
| 152 | + |
| 153 | + |
| 154 | +# Main function |
| 155 | +def logout(): |
| 156 | + st.session_state.logged_in = False |
| 157 | + |
| 158 | + |
| 159 | +def main(): |
| 160 | + global ssh_client, host, username, password |
| 161 | + |
| 162 | + if "logged_in" not in st.session_state: |
| 163 | + host = "localhost" |
| 164 | + st.session_state.logged_in = False |
| 165 | + |
| 166 | + if not st.session_state.logged_in: |
| 167 | + login() |
| 168 | + |
| 169 | + else: |
| 170 | + blasting( |
| 171 | + st.session_state.host, st.session_state.username, st.session_state.password |
| 172 | + ) |
| 173 | + |
| 174 | + |
| 175 | +# Run the main function |
| 176 | +if __name__ == "__main__": |
| 177 | + main() |
0 commit comments