Python Forum
[FLASK] How to structure the code in my case ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[FLASK] How to structure the code in my case ?
#1
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
Reply
#2
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.
Reply
#3
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
Reply
#4
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.
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask return http status code 200 but web browser not recive supernoobs 2 10,491 Dec-29-2018, 09:27 PM
Last Post: Unisoftdev
  string manipulation , code structure maxx 2 2,631 Feb-22-2018, 04:49 PM
Last Post: ebngrtr083
  starting with flask: code does not work Peter_EU 2 3,374 Oct-20-2017, 05:35 PM
Last Post: Peter_EU
  how to place Flask code on a public web server rarevesselt 6 6,893 Oct-04-2017, 07:32 AM
Last Post: rarevesselt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020