Python Forum

Full Version: [FLASK] How to structure the code in my case ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good morning lads, and gents.

I'm currently trying to dig into Flask, and create a simple webpage with it.


For better learning, I also have a project beside of that, where I can use the learned things.

There 2 different problems I have:
1.
Right now I really do not know how to structure the code ?
The tutorial I follow is as follows:
Github: https://github.com/CoreyMSchafer/code_sn...Flask_Blog
Youtube:

Here's the structure:
run.py
-flaskblog
|- static
- main.css
|- templates
- about.html
- login. html etc.
__init__.py
forms.py
models.py
routes.py
site.db

Now I've the webpage. So far so good. Nothing tooo brainbreaking.

Now I want to add an existing project/idea, but I dotn rteally know where to add the code ?

My plan is to login to a specific website, scrape all needed data, store it into the db, and output it on the webpage.
Storage, and output are no problem. The Tutorial mentioned that pretty well.

But where can/should i put the code for the login, and data scrape ?
Should I simply add a folder
|- src ,
and put all necessary code in it ?

2.
How do i start the project then ?
I want to create a button on the website " start " and " stop ". Each of it sets a flag in my db, if the webpage should be scraped or not.

The frontend will be started as follows:
[u]run.py[/u]

from flaskblog import app

# to run flask webserver directly without settings env vars
if __name__ == '__main__':
    app.run(debug=True)
[u]__init__.py[/u]

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt

# INIT
app = Flask(__name__)
app.config['SECRET_KEY'] = '336b60648fd3b8dc33732eb21e968777'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
# DB instance
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)

from flaskblog import routes
Theoretically, I would just add the task handler to __init__ like,
[u]__init__.py[/u]
from src import TaskHandler

data = TaskHandler()
Would that be a good way ?


I'm really thankful for any kind of help, so I can understand such projects structure, better ...

Kr
I would like to recommend the flask mega tutorial:
https://blog.miguelgrinberg.com/post/the...ello-world
it's the most complete flask tutorial that I know of.
this is up to you, what i would do is that i would just add a new file scrapping.py and write the code there.
after that, you can just import the clases/functions from this module to the routes.py if you will want to do new routing for the output page
You can use the structure,but need to get rid of all data as you gone make your own stuff.
Look at Getting Started With Flask same structure,but there starting from scratch.
(May-02-2020, 10:03 AM)Larz60+ Wrote: [ -> ]I would like to recommend the flask mega tutorial:
https://blog.miguelgrinberg.com/post/the...ello-world
it's the most complete flask tutorial that I know of.

Thanks for the flask tutorial !
I'll read through, just to be sure I didnt miss any important stuff in the yt "guide " !
Much appreciated!

(May-03-2020, 05:03 PM)experimental Wrote: [ -> ]this is up to you, what i would do is that i would just add a new file scrapping.py and write the code there.
after that, you can just import the clases/functions from this module to the routes.py if you will want to do new routing for the output page

Thanks for the reply. Since I want to add some logic aswell, I assume one file would be rather messy than structured.

I think I'll just add a " src " folder and throw all files into that, and import the needed functions needed for the output as you suggested.

Again, thanks for your answer.