A MVC analogy in Pythonista terms?
Trying to understand MVCs(Model View Controllers) and I figured Pythonista is structured similarly.
What would be an analogy in Pythonista terms for a model view controller?
The UI that pops up os the view, correct? If so, what are the other two?
Had to look up on Wikipedia what exactly the model, view and comtroller are. In the case of a normal UI program (as you might write on Pythonista) the "controller" is any input (buttons, text fields), the "view" is the output (all UI elements) and the "model" is everything behind the scenes that you do not see as an end user. Some parts of the UI are both view and controller, such as a simple on/off switch - it displays its state (view) and you can toggle it (controller).
I am having a hard time seeing where one picks up and the other leaves off. Wondering if the view is actually graphical elements or code too. Also, if the model is code, is it a function, class, or could it be database (like is it a .json file or .sql file? It seems a controller would be code too, but how does it receive data from the model? What analogous code/modlues would be needed to do this in Pythonista?
The best tutorial I have found was:
I want to create a simple project with this structure to sort of demonstrate how they work. Since we have webviews and such, I could potentially use this structure to create a web app.
Quote from the source:
is telling the model to return the list of books in the selected category (we'll see the model later).
The set method in the line:
Is the controller way to pass data to the view. It sets the books variable to the data returned by the model and makes it accessible to the view.
MVC is a way of organizing an applications code base so that the data (model) and the presentation (view) layers are abstracted away from the logic (controller). Most how-to guides will typically use a web development example when explaining the MVC architecture.
Model refers to the data model and how it is updated. (Typically it is a wrapper around a database). For example, a person model would be a class with the variables first name, last name, age, etc and would include methods for getting and setting these components (as well as saving the data back to the database).
A View is how the data is displayed to the user. In Pythonista, this refers to the UI portion of the app (whether coded or build using the UI editor).
The Controllers handle event requests, contain the logic of the application, and determine how the models work together to make the application run, which in turn calls the views to display the data to the user.
I found an interesting link that tries to explain the concept of MVC using legos if that helps.
Thanks abhenry91! Your first sentence clears up a lot!
So, how would view actions (the actually function for the action) be a controller? You say it is an event handler right?
Also, would data be best stored in a .json file? If so, how would I best access this data in a controller?
I am supposing this:
- data is stored in .json (Model)
- .json file is loaded into a python script which will do stuff to the data. (Controller)
- That stuff is then put in a textview, tableview or some other view based on whether or not the type of data is suited for one or the other. (View)
If this is correct, all I need now is an analogous program (the actual file structure) for Pythonista.
In general, the view receives user input, and based on that input requests operations from the controller. The controller does whatever it has to do to the model, and then updates the view with the results.
Ah, the separation of view and controller makes a lot more sense in a web application, where the view (HTML generation) and controller (HTTP request handling) are two very different parts. With Pythonista's
uithe distinction is a little more confusing, because the view and controller depend on each other - you can send HTTP requests without clicking on a link, but you can't really flip a switch without having the switch. I guess you could modify the variable that the switch would have changed? But that sounds like part of the model, not the controller.
Hmm. Okay. Trying to piece together the puzzles. It seems that an understanding of this structure at least, will help me to structure larger Pythonista apps.
Found a great video tutorial for MVC explanation. Just in case anyone reading this in the future is confused about it.
This post is deleted!last edited by