Cant Deploy basic app

Having a hard time understanding i cant deploy app.

Error Code:
2024-04-02T19:33:44Z error: The deployment failed to initialize due to a configuration or code error. Check the logs tab for more information. For additional support, please post on https://ask.replit.com/c/help/13 and a staff member will take a look.

Screenshots, links, or other helpful context:

import datetime
import math
import random
import flet
import pyperclip
from flet import (
    Column,
    Container,
    ElevatedButton,
    Page,
    Row,
    Text,
    UserControl,
    border_radius,
    colors,
)


class CalculatorApp(UserControl):

  def __init__(self):
    super().__init__()
    self.max_history_length = 10

  def build(self):
    self.reset()
    self.expression = Text(value="", color=colors.WHITE, size=12)
    self.result = Text(value="0", color=colors.WHITE, size=20)
    self.calculation_history = []
    self.history_visible = False

    self.toggle_history_button = ElevatedButton(
        text="Show History",
        bgcolor=colors.BLUE_GREY_100,
        color=colors.BLACK,
        expand=1,
        on_click=self.toggle_history,
        data="toggle_history",
    )
    self.calc_history = Text(value="", color=colors.WHITE, size=12)

    self.clear_history_button = ElevatedButton(
        text="Clear History",
        bgcolor=colors.RED,
        color=colors.WHITE,
        expand=1,
        on_click=self.clear_history,
        data="clear_history",
    )
    self.copy_result_button = ElevatedButton(
        text="Copy Result",
        bgcolor=colors.GREEN,
        color=colors.WHITE,
        expand=1,
        on_click=self.copy_result_to_clipboard,
        data="copy_result",
    )

    return Container(
        width=500,
        bgcolor=colors.BLACK,
        border_radius=border_radius.all(20),
        padding=10,
        content=Column(
            controls=[
                Row(controls=[self.expression], alignment="end"),
                Row(controls=[self.result], alignment="end"),
                Row(controls=[self.calc_history], alignment="end"
                    ),  # Adicionar o controle de texto do histórico
                Row(controls=[self.toggle_history_button]
                    ),  # Adicionar o botão para mostrar/ocultar o histórico
                Row(controls=[self.clear_history_button
                              ]),  # Botão para limpar o histórico
                Row(controls=[self.copy_result_button]),
                Row(controls=[
                    ElevatedButton(
                        text="AC",
                        bgcolor=colors.BLUE_GREY_100,
                        color=colors.BLACK,
                        expand=1,
                        on_click=self.button_clicked,
                        data="AC",
                    ),
                    ElevatedButton(
                        text="+/-",
                        bgcolor=colors.BLUE_GREY_100,
                        color=colors.BLACK,
                        expand=1,
                        on_click=self.button_clicked,
                        data="+/-",
                    ),
                    ElevatedButton(
                        text="%",
                        bgcolor=colors.BLUE_GREY_100,
                        color=colors.BLACK,
                        expand=1,
                        on_click=self.button_clicked,
                        data="%",
                    ),
                    ElevatedButton(
                        text="/",
                        bgcolor=colors.ORANGE,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="/",
                    ),
                ], ),
                Row(controls=[
                    ElevatedButton(
                        text="7",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="7",
                    ),
                    ElevatedButton(
                        text="8",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="8",
                    ),
                    ElevatedButton(
                        text="9",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="9",
                    ),
                    ElevatedButton(
                        text="!",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.random_number,
                        data="random",
                    ),
                    ElevatedButton(
                        text="√",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.square_root,
                        data="sqrt",
                    ),
                    ElevatedButton(
                        text="*",
                        bgcolor=colors.ORANGE,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="*",
                    ),
                ]),
                Row(controls=[
                    ElevatedButton(
                        text="4",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="4",
                    ),
                    ElevatedButton(
                        text="5",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="5",
                    ),
                    ElevatedButton(
                        text="6",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="6",
                    ),
                    ElevatedButton(
                        text="^",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.power,
                        data="power",
                    ),
                    ElevatedButton(
                        text="-",
                        bgcolor=colors.ORANGE,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="-",
                    ),
                ]),
                Row(controls=[
                    ElevatedButton(
                        text="1",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="1",
                    ),
                    ElevatedButton(
                        text="2",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="2",
                    ),
                    ElevatedButton(
                        text="3",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="3",
                    ),
                    ElevatedButton(
                        text="+",
                        bgcolor=colors.ORANGE,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="+",
                    ),
                ]),
                Row(controls=[
                    ElevatedButton(
                        text="0",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=2,
                        on_click=self.button_clicked,
                        data="0",
                    ),
                    ElevatedButton(
                        text=".",
                        bgcolor=colors.WHITE24,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data=".",
                    ),
                    ElevatedButton(
                        text="=",
                        bgcolor=colors.ORANGE,
                        color=colors.WHITE,
                        expand=1,
                        on_click=self.button_clicked,
                        data="=",
                    ),
                ]),
            ], ),
    )

  def clear_history(self, e):
    self.calculation_history = []  # Limpar o histórico de cálculos
    self.update_history()  # Atualizar a exibição do histórico

  def copy_result_to_clipboard(self, e):
    result = self.result.value
    pyperclip.copy(result)  # Copiar o resultado para a área de transferência

  def button_clicked(self, e):
    data = e.control.data
    if self.result.value == "Error" or data == "AC":
      self.result.value = "0"
      self.expression.value = ""
      self.reset()

    elif data in ("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."):
      if self.result.value == "0" or self.new_operand == True:
        self.result.value = data
        self.new_operand = False
      else:
        self.result.value = self.result.value + data

      self.expression.value += data

    elif data in ("+", "-", "*", "/"):
      self.expression.value += " " + data + " "
      self.result.value = self.calculate(self.operand1,
                                         float(self.result.value),
                                         self.operator)
      self.operator = data
      if self.result.value == "Error":
        self.operand1 = "0"
      else:
        self.operand1 = float(self.result.value)
      self.new_operand = True

    elif data in ("="):
      self.expression.value += " ="
      self.result.value = self.calculate(self.operand1,
                                         float(self.result.value),
                                         self.operator)
      self.calculation_history.append(
          (self.expression.value, self.result.value))
      self.calculation_history = self.calculation_history[-self.
                                                          max_history_length:]
      self.reset()

    elif data in ("%"):
      self.result.value = float(self.result.value) / 100
      self.reset()

    elif data in ("+/-"):
      if float(self.result.value) > 0:
        self.result.value = "-" + str(self.result.value)

      elif float(self.result.value) < 0:
        self.result.value = str(
            self.format_number(abs(float(self.result.value))))
    elif data == "CE":
      self.clear_entry(e)

    self.update()

  def toggle_history(self, e):
    self.history_visible = not self.history_visible
    if self.history_visible:
      self.calc_history.visible = False
      self.toggle_history_button.text = "Show History"
      self.update_history()  # Update history display
      self.update()
    else:
      self.calc_history.visible = True
      self.toggle_history_button.text = "Hide History"
      self.update()

  def update_history(self):
    history_text = "History:\n"
    now = datetime.datetime.now()

    history_entries = self.calculation_history[-self.max_history_length:]

    for idx, (expression, result) in enumerate(self.calculation_history,
                                               start=1):
      # Formatar a data e hora como string
      timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
      # Adicionar expressão, resultado, data e hora ao histórico
      history_text += f"{timestamp}: Expression {idx}: {expression} = {result}\n"
    self.calc_history.value = history_text

  def clear_entry(self, e):
    # Encontrar o último espaço na expressão para determinar a posição do início da última entrada
    last_space_index = self.expression.value.rfind(" ")
    if last_space_index != -1:
      self.expression.value = self.expression.value[:last_space_index +
                                                    1]  # Manter a parte da expressão antes da última entrada
    else:
      self.expression.value = ""  # Se não houver espaço na expressão, limpar tudo

    # Limpar a entrada atual
    self.result.value = "0"
    self.new_operand = True  # Reiniciar após limpar a entrada
    self.update()

  def backspace(self, e):
    if len(self.result.value) > 1:
      self.result.value = self.result.value[:-1]
    else:
      self.result.value = "0"
    self.expression.value = self.expression.value[:
                                                  -1]  # Remover último caractere da expressão
    self.update()

  def format_number(self, num):
    if num % 1 == 0:
      return int(num)
    else:
      return num

  def calculate(self, operand1, operand2, operator):

    if operator == "+":
      return self.format_number(operand1 + operand2)

    elif operator == "-":
      return self.format_number(operand1 - operand2)

    elif operator == "*":
      return self.format_number(operand1 * operand2)

    elif operator == "/":
      if operand2 == 0:
        return "Error"
      else:
        return self.format_number(operand1 / operand2)
    elif operator == "^":  # Power operation
      return self.format_number(operand1**operand2)

  def square_root(self, e):
    try:
      result = math.sqrt(float(self.result.value))
      self.result.value = str(result)
      self.expression.value = f"√({self.result.value})"
    except ValueError:
      self.result.value = "Error"
      self.expression.value = "Error: Invalid input"
    self.update()

  def reset(self):
    self.operator = "+"
    self.operand1 = 0
    self.new_operand = True

  def random_number(self, e):
    try:
      result = random.randint(
          0, 100
      )  # Gera um número inteiro entre 0 e 100 (você pode ajustar os limites conforme necessário)
      self.result.value = str(result)
      self.expression.value = f"Rand: {self.result.value}"
    except Exception as ex:
      self.result.value = "Error"
      self.expression.value = "Error: " + str(ex)
    self.update()

  def power(self, e):
    try:
      self.operand1 = float(self.result.value)
      self.operator = "^"
      self.result.value = "0"  # Reinicia o campo de resultado
      self.expression.value += "^"  # Adiciona "^" à expressão
      self.new_operand = True
    except ValueError:
      self.result.value = "Error"
      self.expression.value = "Error: Invalid input"
    self.update()


def main(page: Page):
  page.title = "Calc App"

  # create application instance
  calc = CalculatorApp()

  # add application's root control to the page
  page.add(calc)


flet.app(target=main, view=flet.AppView.WEB_BROWSER)

Also this error code:
forwarding local port 39435 to external port 1104

Does that attempt to display in a VNC window? VNC deployments were not an option last I checked.

Its supposed to open a web page to display it.

Does that open a website on your end, or on the Repl’s end?

Basically, what tab opens when you run it? Does it open a Webview or an Output tab?

It opens the Webview

I see. Does your Repl always run on the same port? If not, that’s the source of your issues.

localPort = 48789
externalPort = 80

In .replit

and when deploy stops working
Logs:

forwarding local port 39435 to external port 1104

So your Repl seems to change ports upon each run, which is your issue.

Checking the flet docs, try flet.app(target=main, view=flet.AppView.WEB_BROWSER, port=8080) instead of flet.app(target=main, view=flet.AppView.WEB_BROWSER), and modify the ports section in .replit to:

localPort = 8080
externalPort = 80
2 Likes

It worked thank you so much. :heart:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.