Выполняю команды:
flet publish main.py --pre
python -m http.server --directory dist
После чего перехожу
127.0.0.1:8000 , страница не прогружается
python -m http.server --directory dist
Serving HTTP on :: port 8000 (http://[::]:8000/) ...
::ffff:127.0.0.1 - - [30/Apr/2024 11:22:47] "GET / HTTP/1.1" 200 -
::ffff:127.0.0.1 - - [30/Apr/2024 11:22:48] "GET / HTTP/1.1" 304 -
::ffff:127.0.0.1 - - [30/Apr/2024 11:22:48] "GET /flutter_service_worker.js?v=1307967035 HTTP/1.1" 200 -
::ffff:127.0.0.1 - - [30/Apr/2024 11:22:51] "GET /flutter_service_worker.js?v=1307967035 HTTP/1.1" 304 -
::ffff:127.0.0.1 - - [30/Apr/2024 11:23:24] "GET /app.tar.gz HTTP/1.1" 200 -
requirements.txt
vk-api
random
sys
time
flet-pyodide
Файл main.py
import flet
import flet as ft
from flet import ElevatedButton, TextField, Column, Row, Page, Text, Image, View
import vk_api
from vk_api.exceptions import VkApiError
import time
import sys
import random
vk_session = None
stop_script = 0
def main(page: ft.Page):
page.title = "Flet App"
page.theme_mode = 'light'
token_field = TextField(label="Token", width=400)
auth_button = ElevatedButton(text="Аутентификация")
start_button = ElevatedButton(text="Запуск", disabled=True)
stop_button = ElevatedButton(text="Остановить", disabled=True)
user_avatar = Image(src="https://evileg.com/media/uploads/2018/07/13/screenshot_20180712_230524.png", width=100, height=100) # Инициализация с пустым источником
user_name = Text()
user_friends_count = Text()
logout_button = ElevatedButton(text="Выйти", visible=False)
output = TextField(label="Терминал", multiline=True, expand=True, read_only=True, max_lines=10)
def auth_click(e):
global vk_session
vk_session = vk_api.VkApi(token=token_field.value)
vk = vk_session.get_api()
try:
auth_button.visible = False
user_info = vk.users.get(fields='photo_100')[0]
friends_count = vk.friends.get(count=0)
user_avatar.src = user_info['photo_100'] # Обновление источника изображения
start_button.disabled = False
#stop_button.disabled = False
logout_button.visible = True
token_field.visible = False
user_name.value = f"{user_info['first_name']} {user_info['last_name']}"
user_friends_count.value = f"Количество друзей: {friends_count['count']}"
output.value += f"Имя: {user_info['first_name']}, Фамилия: {user_info['last_name']}, Количество друзей: {friends_count['count']}\n"
except VkApiError as e:
user_name.value = "Ошибка аутентификации"
output.value += f"Ошибка аутентификации: {e}\n"
user_friends_count.value = ""
page.update()
def start_click(e):
global stop_script
global vk_session
stop_script = 0
output.value += "Скрипт запущен!\n"
page.update()
vk_session = vk_api.VkApi(token=token_field.value)
vk = vk_session.get_api()
start_button.disabled = True
stop_button.disabled = False
try:
candidates = vk.friends.getSuggestions()
candidate_ids = candidates['items']
except VkApiError as e:
output.value += f'Ошибка при получении списка возможных друзей: {e}\n'
return
for candidate_id in candidate_ids:
if stop_script >= 5:
output.value += "Скрипт остановлен!\n"
break
time.sleep(random.randint(5,10))
try:
vk.friends.add(user_id=candidate_id['id'])
output.value += f'Запрос на добавление {candidate_id["first_name"]} в друзья выполнена успешно\n'
except VkApiError as e:
stop_script += 1
if stop_script >= 5:
output.value += "Скрипт остановлен!\n"
break
output.value += f'Ошибка при отправке запроса {candidate_id["first_name"]} на добавление в друзья: {e}\n'
page.update()
output.value += 'Отправка запросов на добавление в друзья выполнена успешно\n'
page.update()
def stop_click(e):
global stop_script
stop_script = 5
start_button.disabled = False
stop_button.disabled = True
#output.value += "Скрипт остановлен!\n"
page.update()
def logout_click(e):
global vk_session
try:
# Assuming 'vk' is the authorized VK API object
vk_session = None
output.value += "Вы успешно вышли из системы!\n"
# Reset the UI elements to their default state
auth_button.visible = True
start_button.disabled = True
stop_button.disabled = True
logout_button.visible = False
token_field.visible = True
user_avatar.src = "" # Reset the avatar image source
user_name.value = ""
user_friends_count.value = ""
except VkApiError as e:
output.value += f"Ошибка при выходе: {e}\n"
page.update()
auth_button.on_click = auth_click
start_button.on_click = start_click
stop_button.on_click = stop_click
logout_button.on_click = logout_click
page.add(
Column(
[
Row([token_field, auth_button]),
user_avatar,
user_name,
user_friends_count,
logout_button,
Row([start_button, stop_button]),
output
]
)
)
flet.app(target=main)
#flet.app(target=main, view=ft.AppView.WEB_BROWSER)