What is Sanic in Python?

Posted in /  

What is Sanic in Python?
vinaykhatri

Vinay Khatri
Last updated on December 10, 2024

    Right now, Flask and Django are the two leading frameworks of Python, and these two frameworks are mostly used for development options. But when we say fast and better-performing Python frameworks, there Python has many other web frameworks to offer, and Sanic is one of those. Sanic is a Flask-like Python web framework that is asynchronous in nature and built for fast performance.

    It is one of the best alternatives to Flask for building efficient, simple, and scalable REST APIs. In this introductory article on Sanic, we will learn about Sanic and see how to use sanic in Python with some examples.

    What is Asynchronous Programming in Sanic

    Frameworks like Django and Flask follow synchronous programming where their applications work on request and response criteria. In synchronous programming, the Framework handles the request and response action in sequential order, in which if the user makes two API requests to the application, the responses will be in a sequential manner, where the second request will be fetched after the completion of the first request.

    But, Sanic is an Asynchronous framework and follows Asynchronous programming. Asynchronous programming was introduced in Python 3.5. That's why Sanic is only available for Python 3.5 and above versions.

    In Asynchronous programming, the program is executed in multiple parallel threads. Although Python executes its program in a single thread, the asynchronous only give an illusion of parallelism, in which it actually executes one request at a time, but it shifts from one to another if one request is taking more time for its response.

    Features of Python Sanic Framework

    • Sanic is a Python 3 web framework that is only available for python 3.5 and above versions.
    • It is an asynchronous framework that uses Python async/await syntax.
    • It is a very fast framework.
    • It is a minimal framework, and like Flask, it can build simple web applications with a single Python script.
    • Sanic is widely used with Python Fast API to build robust web REST APIs.
    • Support ASGI web server for development.

    Advantages of Sanic Framework

    • It is a lightweight Python framework that makes it fast and flexible at the same time.
    • It supports Asynchronous programming,
    • It is a very powerful tool for building REST APIs.

    Disadvantages of Sanic Framework

    • It does not support any default templating engine.
    • It is not as powerful as other popular Python frameworks like Flask and Django.
    • It also does not have many external extensions like Flask and Django.

    Get Started with Python Sanic Framework

    As Sanic is a third-party open-source Python framework, we first need to install it for our Python environment. We can either use a Python virtual environment or directly install sanic for our global Python. To install sanic, we can use the Python pip install terminal command.

    pip install sanic

    After successfully installing sanic, let us write our first Python Sanic Hello World Program.

    Hello World web app with Python Sanic Framework

    We can build a simple hello world program in sanic with a single Python script. Now let's create an app.py file and start writing our sanic hello world program. #app.py

    from sanic import Sanic, response
    
    app = Sanic(__name__)
    
    @app.route("/")
    async def hello_world(request):
       return response.text("Hello World! Welcome to sanic")
    
    if __name__ == '__main__':
       app.run(host='0.0.0.0', port=8000, debug=True)

    Now run the app.py file on your terminal, and it will run the web app on your local host at port 8000. After executing the app.py you can visit http://0.0.0.0:8000 on your browser, and you will see a similar output.

    In the above program, we render the text " Hello World! Welcome to sanic " with sanic response.text() method. We can also render JSON and HTML text with response.json() and response.html() methods.

    Example Show JSON data with Sanic

    from sanic import Sanic, response
    
    app = Sanic(__name__)
    
    @app.route("/")
    async def json_data(request):
       return response.json({'name':'rahul', 'age':20})
    
    if __name__ == '__main__':
       app.run(host='0.0.0.0', port=8000, debug=True)

    Output

    Serve HTML file with Sanic framework

    A web application is a combination of backend scripting and front-end pages. We can use the response.file() function to render HTML pages using sanic.

    #index.html

    <!DOCTYPE html>
    <html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        <h1> Welcome to Sanic </h1>
        <p>This is my first web page with sanic</p>
    </body>
    </html>

    #app.py

    from sanic import Sanic, response
    
    app = Sanic(__name__)
    
    @app.route("/")
    async def index_page(request):
       return await response.file('index.html')   #index.html should be in the same directory of app.py
    
    if __name__ == '__main__':
       app.run(host='0.0.0.0', port=8000, debug=True)

    Conclusion

    Now let's conclude this introductory sanic article with final thoughts. In this article, we learned about Python's Sanic web framework and how to install and work with it. Sanic is a very fast framework, and because of its asynchronous programming support, it is primarily used to work with RESTful APIs. It is only supported by Python 3.5+ versions because asynchronous was introduced to Python in the Python 3.5 version.

    To know more about Python Sanic Framework, you can visit its official documentation or click here .

    People are also reading:

    FAQs


    Sanic is a web server and web framework for Python 3.5+. It is designed with the uv event loop for fast HTTP responses through asynchronous request handling.

    Sanic is fast becaue it is based on the uv event loop rather than Python's asyncio’s default event loop.

    Some major features of Sanic include a highly scalable, fast web server, production-ready, intuitive API design, and ASGI compliant.

    Leave a Comment on this Post

    0 Comments