Cover image for From Rails to Flask: Leveraging Ruby Expertise in Python Development

From Rails to Flask: Leveraging Ruby Expertise in Python Development

In the evolving landscape of software development, transitioning between programming languages and frameworks is a journey that has expanded my skill set and deepened my understanding of different paradigms. Coming from a Ruby on Rails background, I embarked on a new adventure into the Python world with its Flask framework. This shift not only allowed me to explore Python's  syntax and powerful libraries, but also led me to develop a practical application -- a simple yet effective note-taking platform.

Understanding the Application's Architecture

The application is structured to utilize Flask's capabilities, using Blueprints to separate and organize code -- similar to how Rails uses MVC architecture. This separation enhances maintainability and scalability, concepts I already appreciated in Rails! 😄

Application Initialization and Configuration

In Flask, the application starts in the __init__.py file, where the app and its configurations are defined. This is somewhat similar to Rails, where various setups for development, testing, and production are specified. In Flask, you typically set configurations like the secret key and database URI directly:

    app = Flask(__name__) # root path
    app.config['SECRET_KEY'] = "enter random key" # Flask signs session cookies with this
    app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{DB_NAME}"

This setup initializes the Flask app and configures it to use SQLAlchemy, Flask's ORM equivalent to Rails' ActiveRecord, for handling database operations.

Database Models

Transitioning to defining data models, Flask uses SQLAlchemy, which is quite similar to declaring models in Rails. In Rails, you would use ActiveRecord to define your models and relationships. Flask with SQLAlchemy follows a similar pattern but within the Python syntax:

class Note(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  data = db.Column(db.String(10000))
  date = db.Column(db.DateTime(timezone=True), default=func.now())#func gets current date and time and stores default value for date
  user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

This Note model stores notes with a reference to a user, like to how models might be set up in a Rails application using associations.

Routing and View Functions

Flask routes are defined in views similar to Rails routes but are typically closer to the controller logic, combining both routing and view handling in the same function. This is like a blend of Rails' routes file and controller actions:

@views.route('/notes', methods=['GET', 'POST'])
def home():
    if request.method == "POST":
        note = request.form.get("note")  # Gets the note from the HTML
          # Logic to display notes
    return render_template('notes.html')

Here, the home function acts similarly to a Rails controller action, handling both the retrieval and submission of notes based on the HTTP method.

Reflecting on the Development Process

The process of building this Flask application let me apply principles from Rails in a new context, demonstrating the universality of web development frameworks. From setting up the application and modeling data to handling requests and serving responses, the transition from Rails to Flask showcased how foundational concepts could adapt to different tech stacks.

Conclusion

By sharing this journey, I hope to inspire other developers to explore new languages and frameworks, understanding that the core principles of software development often remain consistent, even as the tools and environments evolve. This project not only enhanced my skills but also provided a functional tool for daily use, highlighting the practical benefits of continuous learning in the field of software development.

Here is the link to the application's GitHub repo: https://github.com/karanm645/flask_notes_management

Comments, or any advice you'd like to share? Email me at karanm645@gmail.com!
Back