0

I have an app i am making and i would like to hold all python code, css and js assets inside a folder and name it appropriately so that one person can do a complete module,test it and upload it to the main project.

I am planning to have something like this structure

index.py

global_assets
global_classes
global_html_components
home(assets(js,css,images),home.html,home_widget_1.html,home_widget_2.html,home_widget_3.html,controller.py,model.py)
services
products
dashboard

I have this script.py

from flask import *  
app = Flask(__name__)  

@app.route('/user/<uname>')  
def message(uname):  
      return render_template('message.html',name=uname)  
if __name__ == '__main__':  
   app.run(debug = True)  

and message.html

<html>  
<head>  
<title>Message</title>  
</head>  
<body>  
<h1>hi, {{ name }}</h1>  
</body>  
</html> 

I want my app to have one entry at index.py such that i can visit urls of any of my modules and the page displays for instance in the code above

http://localhost:5000/home/user/admin where home is one of the folders i named above.

Is this kind of structure possible in flask?

Gandalf
  • 1
  • 29
  • 94
  • 165
  • IIUC, you're looking for [blueprints](https://flask.palletsprojects.com/en/1.0.x/blueprints/). However, you would typically have more than 1 folder associated with a blueprint - you'd have a `templates` subdirectory called `home`, and routes/models/forms would be in the original folder you described. You'd also probably want separate subdirectories for js/css (if your site doesn't cover these requirements generally). See the [Python Community Page](https://github.com/sopython/sopython-site) for an example, where each primary page is its own directory. – roganjosh Oct 27 '19 at 10:30
  • Then you set your `url_prefix` to home when you import the blueprint into the main app. That's probably a lot more sustainable than trying to get everything completely contained in a single directory – roganjosh Oct 27 '19 at 10:32
  • @roganjosh the first option for sopython wont work for me. What i am looking for is everything contained in its own folder. have you tried flask with wsgi? – Gandalf Oct 27 '19 at 18:29
  • No, I haven't. I just use gunicorn which is a single command line for production – roganjosh Oct 27 '19 at 19:00
  • @roganjosh actually the blueprint idea is pretty good. I found a related question for someone thinking in the same lines https://stackoverflow.com/questions/11994325/how-to-divide-flask-app-into-multiple-py-files I found the only way to do what i wanted is to use cgi as described here https://stackoverflow.com/questions/9664634/running-python-scripts-like-php – Gandalf Oct 27 '19 at 19:46
  • I'm not so sure about the 2nd link, not sure I can comment. But it looks like I might have pushed you in the right direction, so hopefully you'll be able to post your own answer if nobody can help you before that :) Good luck mate. – roganjosh Oct 27 '19 at 20:07

0 Answers0