Python as a Full stack – Write your entire app in Python

Python as a Full stack – Write your entire app in Python

Python is now the number one choice for programmers working in the fields of machine learning and artificial intelligence. This has led to new web frameworks that allow you write entire apps with no Javascript, HTML or CSS

Python already has Django and Flask web frameworks, but they require a lot of JavaScript or HTML. There are more powerful and easier ways to develop web applications using Python today, such as Pynecone, Flet and Gradio.


No alt text provided for this image

Pynecone is the best web framework for Python. It’s a full-stack web framework that allows you to build complete apps without any JavaScript or HTML. Pynecone uses its own unique templating language called NTP, which is a simple yet powerful way to write templates. The syntax is easy to learn and allows you to write entire websites with only Python code.

Pynecone also comes with a built-in development server and database system. You can use Pynecone to build any type of web application, including websites, APIs and single-page applications (SPAs). The latest version contains many new features, such as an improved ORM and a more flexible template system

Sample code for writing a counter app

import pynecone as pc
class State(pc.State):
count: int = 0
def increment(self):
self.count += 1
def decrement(self):
self.count -= 1
def index():
return pc.hstack(
pc.button(
"Decrement",
color_scheme="red",
border_radius="1em",
on_click=State.decrement,
),
pc.heading(State.count, font_size="2em"),
pc.button(
"Increment",
color_scheme="green",
border_radius="1em",
on_click=State.increment,
),
)
app = pc.App(state=State)
app.add_page(index)
app.compile()

No alt text provided for this image


Benefits

  • Build complex multi-page apps.
  • No need of Javascript and HTML

No alt text provided for this image

Flet is a tool for developers to build realtime apps in Python. Flet is a framework for adding server-driven user interfaces (SDUIs) to existing Flutter apps, or for building standalone web, mobile, and desktop apps with Flutter UI.

Add an interactive FletApp widget to your Flutter app whose content is controlled by a remote Python script. It is an ideal solution for building non-core or frequently changing functionality such as product catalog, feedback form, in-app survey or support chat. Flet enables your team to ship new features faster by reducing the number of App Store validation cycles. Just re-deploy a web app hosting a Python script and your users will get an instant update!

On the server side Flet provides an easy to learn programming model that enables Python developers without prior Flutter (or even front-end) experience to participate in development of your larger Flutter app or build their own apps with Flutter UI from scratch.

Install flet Python module 

Flet requires Python 3.7 or above. To start with Flet, you need to install flet module first:

pip install flet        

Create Python program #

Create a new Python program using Flet which will be driving the content of FletApp widget.

Let's do a simple counter.py app similar to a Flutter new project template:

import flet
from flet import IconButton, Page, Row, TextField, icons
def main(page: Page):
   page.title = "Flet counter example"
   page.vertical_alignment = "center"
   txt_number = TextField(value="0", text_align="right", width=100)
   def minus_click(e):
       txt_number.value = int(txt_number.value) - 1
       page.update()

   def plus_click(e):
       txt_number.value = int(txt_number.value) + 1
       page.update()
   page.add(
       Row(
           [
               IconButton(icons.REMOVE, on_click=minus_click),
               txt_number,
               IconButton(icons.ADD, on_click=plus_click),
           ],
           alignment="center",
       )
   )
flet.app(target=main, port=8550)
No alt text provided for this image
No alt text provided for this image

Gradio is a user-friendly Python library that allows developers to quickly and easily create custom user interfaces for machine learning models. With Gradio, you can build interactive interfaces that allow users to input data, view model predictions, and explore the results of your machine learning algorithms.


Gradio provides a simple, intuitive interface for creating user interfaces. You can create an interface for your machine learning model with just a few lines of code. To get started, you'll need to install the Gradio library by running the following command in your terminal:

pip install gradio

Once you have Gradio installed, you can begin building your user interface. To create a simple interface for a machine learning model that classifies images, for example, you might use the following code:

import gradio as gr
import tensorflow as tf
import numpy as np
# load the model
model = tf.keras.models.load_model("my_model.h5")
# define the function for making predictions
def predict_image(img):
  img = img.reshape(-1, 28, 28, 1) # resize the image
 img = img.astype('float32') / 255.0 # normalize the pixel values
  pred = model.predict(img)[0] # make the prediction
  return {str(i): float(pred[i]) for i in range(10)}

# create the interface
iface = gr.Interface(
 fn=predict_image, 
 inputs=gr.inputs.Image(shape=(28, 28)), 
 outputs=gr.outputs.Label(num_top_classes=3),
 interpretation="default"
)

# launch the interface
iface.launch()

Benefits

  • Allows you to customize your interface in a variety of ways
  • Powerful and easy-to-use tool for building user interfaces for machine learning models.
  • Voice input , text speech are possible.

Conclusion

Gradio, Flet, and PyEncone are all Python libraries that provide tools for creating user interfaces for machine learning models. While they share some similarities, there are also some key differences between these libraries.

Gradio is a library that is specifically designed for creating user interfaces for machine learning models. It provides a simple, intuitive interface for building interfaces, and supports a wide range of input and output types, including text, images, and audio. Gradio also includes features for customizing the layout and style of the interface, making it easy to create polished and professional-looking interfaces.

Flet is a similar library that also provides tools for building user interfaces for machine learning models. However, Flet is more focused on the data preparation and model training stages of the machine learning workflow. It provides tools for data augmentation, data preprocessing, and model training, as well as a variety of pre-built models that can be used for different tasks. Flet also includes tools for building and deploying machine learning models to production environments.

PyEncone is a library that is designed for creating user interfaces for Python scripts more generally, including machine learning scripts. It provides a simple, web-based interface for running Python scripts, and includes features for managing input and output, handling errors, and monitoring script execution.

In terms of their strengths and weaknesses, Gradio is a great choice if you are looking to quickly and easily build polished interfaces for machine learning models. It is easy to use and provides a wide range of input and output types. Flet, is Flutter based python interface better choice for building complex app in mobile and web. Finally, PyEncone is a more general-purpose library that can be used for a variety of Python scripts, including machine learning scripts and supports multipage web application

Sandeep Soni

Full Stack Software Developer (Python) | Graphic Designer | MySQL | SQL | NoSQL | Python Programming | Back-end & Front-end Developer | Customer Handler | Quality Checker | Data Analyst | Salseman | Logics Builder

1y

Do you have job?

Like
Reply
Jose Chay

Full Stack Engineer | Telus Digital Solutions | Backend | Python & NodeJs | Frontend | React & React Native & Angular | AWS Cloud Practitioner Certified

1y

Wow good article

Like
Reply

To view or add a comment, sign in

More articles by Gopi Narayanaswamy

Insights from the community

Others also viewed

Explore topics