Repository: h2oai/wave
File: studio/studio.py (Python)

Code before:
                editor.open_file(q, project.entry_point)
                requirements_file = open('project/requirements.txt', 'r') if os.path.exists(
                    'project/requirements.txt') else None
                if requirements_file:
                    await show_packages_dialog(q)
                    q.client.task = asyncio.create_task(pip(
                        q,
                        'install',
                        '-r',
                        'project/requirements.txt',
                        None,
                        'Installing packages from requirements.txt',
                        f'Packages from requirements.txt installed successfully.',
                        f'Error installing requirements.txt.',
                        None,
                        on_requirements_install_error
                    ))
            else:
                q.page['meta'].dialog.items = [
                    ui.message_bar(type='error', text='There must be exactly 1 root folder.'),
                    ui.button(name='import_project', label='Import again', icon='Upload'),
                ]
    elif q.args.show_packages_dialog:
        await show_packages_dialog(q)
    elif q.events.package_dialog and q.events.package_dialog.dismissed:
        q.page['meta'].dialog = None
    elif q.args.show_add_requirements:
        q.page['meta'].dialog.items[2] = ui.file_upload(name='upload_requirements', file_extensions=['txt'],
                                                        label='Install packages',
                                                        tooltip='Packages from requirements.txt will be added to already installed packages.')
    elif q.args.upload_requirements:
        os.mkdir('project/tmp')
        file = await q.site.download(q.args.upload_requirements[0], os.path.join(os.getcwd(), 'project/tmp'))
        shutil.copy(file, 'project/requirements_tmp.txt')
        shutil.rmtree('project/tmp')
        q.client.task = asyncio.create_task(pip(
            q,
            'install',
            '-r',
            'project/requirements_tmp.txt',
            None,
            'Installing packages from requirements.txt',
            f'Packages from requirements.txt installed successfully.',
            f'Error installing requirements.txt.',
            on_requirements_install_success,
            on_requirements_install_error
        ))
    elif q.args.show_add_package_fields:
        q.page['meta'].dialog.items[2] = ui.inline(items=[
            ui.textbox(name='package_name', label='Package name', required=True),
            ui.textbox(name='package_version', label='Version', placeholder='(latest)'),

Review comment (suggestion):
There is always going to be just one dialog so no need to distinguish between `package_dialog` and `dialog` in terms of events. WIll make code simpler as there would be no need for separate handlers.

Code after:
                project.entry_point = file_utils.find_main_file(project.dir)
                q.client.opened_file = project.entry_point
                await render_code(q)
                editor.update_file_tree(q, project.dir)
                await q.page.save()
                editor.open_file(q, project.entry_point)
                if os.path.exists('project/requirements.txt'):
                    await show_side_panel(q)
                    q.client.task = asyncio.create_task(install(q, 'project/requirements.txt'))
            else:
                q.page['meta'].dialog.items = [
                    ui.message_bar(type='error', text='There must be exactly 1 root folder.'),
                    ui.button(name='import_project', label='Import again', icon='Upload'),
                ]
    elif q.args.show_side_panel:
        await show_side_panel(q)
    elif q.args.tab_menu:
        q.page['meta'].side_panel.items = get_side_panel_content(q.args.tab_menu)
    elif q.args.upload_requirements:
        file_path = os.path.join(os.getcwd(), 'project')
        file = await q.site.download(q.args.upload_requirements[0], file_path)
        os.rename(file, os.path.join(file_path, 'requirements.txt'))
        q.client.task = asyncio.create_task(install(q, 'project/requirements.txt'))
    elif q.args.add_package:
        q.client.task = asyncio.create_task(install(q, q.client.package_name, q.args.package_version))
    elif q.args.package_name is not None:
        q.client.package_name = q.args.package_name
        q.page['meta'].side_panel.items[3].button.disabled = not q.args.package_name
    elif q.events.table and q.events.table.select is not None:
        q.client.selected_packages = q.events.table.select
        q.page['meta'].side_panel.items[1].button.disabled = not bool(q.events.table.select)
    elif q.args.remove_selected:
        q.client.task = asyncio.create_task(uninstall(q, q.client.selected_packages))
    elif q.args.cancel_pip_task:
        q.client.task.cancel()
    elif q.args.finish_message_dismiss:
        await on_pip_finish(q)
    elif q.args.dropdown:
        q.user.view = q.args.dropdown
        if q.args.dropdown == 'code':
            q.page['preview'].box = ui.box('main', width='0px')
            q.page['code'].box = ui.box('main', width='100%')
        if q.args.dropdown == 'split':
            q.page['preview'].box = ui.box('main', width='100%')
            q.page['code'].box = ui.box('main', width='100%')
        if q.args.dropdown == 'preview':
            q.page['preview'].box = ui.box('main', width='100%')
            q.page['code'].box = ui.box('main', width='0px')
    elif q.args.console:
        q.page['preview'].box = ui.box('main', width='100%')
        q.page['code'].box = ui.box('main', width='0px')