I built a chrome extension that can explain code blocks to you right from the site: Introducing Syntaxify ✨

I built a chrome extension that can explain code blocks to you right from the site: Introducing Syntaxify ✨

·

5 min read

Introduction

As a developer, we often find ourselves struggling with understanding complex code blocks on different sites and resources and spend hours trying to debug and understand them. Reading code written by others is a cumbersome task but it doesn't have to be! What if I told you that there is an extension that can explain those code blocks to you in a matter of seconds?

Introducing Syntaxify, a powerful tool that can help you understand complex code blocks in a simple and easy-to-understand way. Syntaxify lets you select code blocks right from the site and then get its explanation in seconds using MindsDB's excellent NLP integrations with OpenAI.

This extension is especially useful for beginners who are just starting out with coding and need some extra help understanding complex concepts. It can also benefit experienced developers who want to save time and speed up their workflow.

Want to learn more about this tool and how it was made? Keep reading!

Creating the Extension

The extension was made by me from scratch and it was not at all an easy task. Being my first chrome extension, I had to go through a lot of resources and references to create it. The first step to creating this extension is to create an NLP model using MindsDB.

  • Create a free account on MindsDB to get access to their cloud dashboard.

  • Create your OpenAI model using the Editor, you can change the name of columns and model to whatever you want.

      CREATE MODEL code_explainer
      PREDICT explanation
      USING
          engine = 'openai',              
          prompt_template = 'provide a concise and easy to understand explanation of the given code block:{{block}}, make sure it can be easily understood even by beginners';
    
  • Once you have created your model, It's time to integrate it with your extension, I made my backend using Flask (Python) and used the MindsDB Python SDK to connect with my MindsDB instance.

    We are using Flask to create the API. Firstly, we need to get the URL arguments that contain the code block and then we feed the code block data to our MindsDB model using the Python SDK which returns a DataFrame. The DataFrame contains our code explanation which we can return as response from the API.

    from flask import Flask, request
    import mindsdb_sdk as mdb
    import urllib.parse
    import dotenv

    app = Flask(__name__)

    dotenv.load_dotenv()

    EMAIL = dotenv.get_key(".env", "EMAIL")
    PASSWORD = dotenv.get_key(".env", "PSWD")


    def get_explanation(code: str) -> str:
        """Get explanation for a given code

        Args:
            code (str): code to get explanation for

        Returns:
            str: explanation for the given code
        """
        server = mdb.connect(login=EMAIL, password=PASSWORD)
        project = server.get_project("mindsdb")

        # replace double quotes with single quotes

        code = code.replace('"', "'")

        # // escape single quotes and double quotes and semicolons from code by adding \ before them

        escapedCode = code.replace("'", "\\'").replace('"', '\\"').replace(";", "\\;")

        q = project.query(
            f"""
            SELECT article, highlights
            FROM cex
            WHERE article = "{escapedCode}"
            USING max_tokens = 1000;
        """
        )

        df = q.fetch()
        result = df.iloc[0]["highlights"]
        print(result)
        return result


    @app.route("/")
    def explainer():
        print(EMAIL, PASSWORD)
        code = request.args.get("code")
        if code:
            decoded_code = urllib.parse.unquote(code)
            return get_explanation(decoded_code)

        return "No code provided"


    if __name__ == "__main__":
        app.run(debug=True)
  • Once your backend is ready, you can start building the extension and connect your backend to it, Since the extension code is a bit complex, it would be unnecessary to include it here, but the source code can be seen here

Why & How Did I Use MindsDB?

I need two important things to build this tool - a database and an AI model. And MIndsDB is the perfect mix of both - a database with inbuilt AI-ML capabilities. Along with being able to run ML models right in your database, I could even use NLP through HuggingFace and OpenAI models and that was exactly what I needed.

The tool uses MindsDB to store all the generated explanations as well as provides the explanations using OpenAI's model! In the future, I would like to implement authentication in the app and MindsDB will be used to store user data as well! It's amazing how you can leverage MindsDB to replace most of the backend tools you might need to build an AI/ML app.

Usage

Here comes the most interesting part, you can use the extension right in your browser and it takes just a few steps.

  • Clone the Syntaxify GitHub Repository locally using git CLI or downloading it as zip file.

  • Inside the repository, you would see a dist folder. That's the extension that we need to load.

  • Open Chrome, and then go to the Settings > Extensions and enable **Developer Mode**.

  • Now click on Load Unpacked and then select the dist folder that we cloned in step 1.

  • Your extension is loaded and ready to use now. Watch this video to learn how to use it!

Conclusion

In conclusion, Syntaxify is a game-changing tool that can make the life of developers easier by providing an easy-to-understand explanation of complex code blocks. It can help beginners learn to code faster and more efficiently while also benefiting experienced developers by saving them valuable time and effort. With this extension, you can improve your coding skills, become a more productive developer, and ultimately, create better software.

Thanks a lot Hashnode and MindsDB for giving me this amazing opportunity to work on this project. I learned a lot from this hackathon build.

Did you find this article valuable?

Support ashish by becoming a sponsor. Any amount is appreciated!