Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
The Fool
Oct 16, 2003


fwiw I absolutely hate the tone of parts of the foundation documentation

Adbot
ADBOT LOVES YOU

CarForumPoster
Jun 26, 2013

⚡POWER⚡

Oysters Autobio posted:

Just getting into learning flask and my only real question is how people generally start on their HTML templates. Are folks just handwriting these? Or am I missing something here with flask?

Basically I'm looking for any good resources out there for HTML templates to use for scaffolding your initial templates. Tried searching for HTML templates but generally could only find paid products for entire websites, whereas I just want sort of like an HTML component library that has existing HTML. i.e. here's a generic dashboard page html, here's a web form, here's a basic website with top navbar etc.

Bonus points if it already has Jinja too but even if these were just plain HTML it would be awesome.

edit: additionally, has anyone used flask app builder before? The built in auth and admin panels are mighty appealing but I'm worried about locking into a heavily opinionated framework (why I chose flask over say Django)

If your goal is to make dashboards or web apps with a few pages for internal tools I highly recommend Dash. No HTML, Python objects only including a Markdown object which makes things more readable than templates imo. Theres a well supported Bootstrap package called dash bootstrap components which gives you pretty bootstrap things and has a Bootstap navbar. I wouldn’t consider it any more opinionated than Flask, which doesn’t mean none, certainly no where near Django.

No need for Jinja because again, Python only.

CarForumPoster fucked around with this message at 22:11 on Mar 19, 2024

Oysters Autobio
Mar 13, 2017
Yeah I did consider Dash and I have used plotly but I'm a bit skeptical I guess it. Seemed a bit more like Streamlit where it'd be good for demos and little mini apps but prob doesn't have all the other ecosystem of stuff like Flask. Definitely open for people's experience with Dash

I did know about Bootstrap but wasn't sure if it was still well regarded and/or suggested given its age. Probably the best one to use given how much existing examples out there exist.

Anyone have experience with Flask AppBuilder? I know airflow + superset was built with it but I think the same guys made it so dunno how well regarded it is. The LDAP security integration and admin panels look good but would be curious if it locks you into a really opinionated framework like Django or if you still have decent flexibility.

DoctorTristan
Mar 11, 2006

I would look up into your lifeless eyes and wave, like this. Can you and your associates arrange that for me, Mr. Morden?
Dash is a lot more flexible than Streamlit

Fender
Oct 9, 2000
Mechanical Bunny Rabbits!
Dinosaur Gum

Oysters Autobio posted:

Just getting into learning flask and my only real question is how people generally start on their HTML templates. Are folks just handwriting these? Or am I missing something here with flask?

Basically I'm looking for any good resources out there for HTML templates to use for scaffolding your initial templates. Tried searching for HTML templates but generally could only find paid products for entire websites, whereas I just want sort of like an HTML component library that has existing HTML. i.e. here's a generic dashboard page html, here's a web form, here's a basic website with top navbar etc.

Bonus points if it already has Jinja too but even if these were just plain HTML it would be awesome.

Chat-GPT will also happily spit out some Flask boilerplate templates for you. I've never tried to get it to do anything more complex than laying out some really basic crud stuff, but it does ok-ish.

Zoracle Zed
Jul 10, 2001

Oysters Autobio posted:

FastAPI swagger page generation is pretty cool but it's not like a drop-in or anything to my knowledge (ie you have to build the API with it from the beginning).

I've been exploring python docs stuff and have found that mkdocs and mkdocs material have a big ecosystem of plugins and the like, all built on markdown instead of rst. Might have some API docs gen options.

Thanks for this by the way! I've been playing around with it and like it a lot. Markdown instead of rst is wayyy more ergonomic for me and `mkdocstrings` is the plugin does the automatic API docs generation exactly like I'd want it to, including out of the box support for parsing type annotations from function signatures

StumblyWumbly
Sep 12, 2007

Batmanticore!
Does anyone have a good best practice for type hinting on widely shared stuff? It definitely clears things up, but a lot of it is so new and it seems like the choices are to have people always on the latest version or not use stuff like the Filename type for a few years.
Oh never mind it sounds like Filename is our own specific type. I'm still interested in the question, but it's easier since type hints aren't changing as much as I thought

StumblyWumbly fucked around with this message at 18:23 on Mar 22, 2024

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug

StumblyWumbly posted:

Does anyone have a good best practice for type hinting on widely shared stuff? It definitely clears things up, but a lot of it is so new and it seems like the choices are to have people always on the latest version or not use stuff like the Filename type for a few years.
Oh never mind it sounds like Filename is our own specific type. I'm still interested in the question, but it's easier since type hints aren't changing as much as I thought

I'm a little confused by this - type hinting is widely supported and stable, it's in the PEP. For anything widely shared, I think it's extremely important. You should basically do it 100% of the time - the fact that you didn't know that Filename was your own class is a great indication of where type hinting would have helped, for example. One of Python's biggest weaknesses in practice is that you can write extremely unreadable unintelligible code extremely quickly, and type hinting helps offset that significantly. At my work I very routinely reject PRs until type hinting is added, and I think it's more important than docstrings (although realistically there's some overlap between the two).

That being said, there's plenty of times where type hinting becomes super unwieldy, and mostly it comes down to edge case stuff like dictionary dispatch using functions or heterogenous dictionaries or things like that (or just super complex nested dicts). On one hand, that's a good reason to avoid some of those things sometimes - if it's complicated to document, it's going to be complicated to unravel later. I think ultimately you should use them where appropriate internally and don't expose them to the outside directly, rather than avoiding them altogether - dogma in development is rarely ever true 100% of the time.

Except ISO-8601 - that's the one true path and anyone who uses another datetime format is a heretic and does not deserve to draw breath.

Edit:

Also, the PEP8 for Type Hinting is from 2014, so it's definitely not 'new'. https://peps.python.org/pep-0484/. Obviously support for it isn't as old as the actual PEP, but at this point all major IDEs and analysis tools support it that I've seen.

Falcon2001 fucked around with this message at 00:18 on Mar 23, 2024

nullfunction
Jan 24, 2005

Nap Ghost
Yeah, most of the typing stuff you'd likely reach for has been a part of the language for years now. Sometimes you'll have a dependency that requires a particular Python version, which informs the ceiling on features available to you. If what you produce is a module, you'll sometimes find that consumers of that module have some sibling dependency that is stuck on an older Python and limits your ability to use the features you want.

If you're looking for a baseline here's what I tend to do for all greenfield code and all code being refactored on an existing system.

1. Every argument should have a type annotation, every function should have a return type annotation.
2. Lists and dicts should always be annotated with what they are expected to contain in arguments. If you declare one in a function body (assuming Python >= 3.9) it should be annotated if declared empty or the type can't be inferred from the assignment expression. list isn't enough, list[str] is always better.
3. Complex or deeply-nested structures get turned into something more manageable either by aliasing or dataclasses. Type aliasing has existed since 3.5, though you'll have to pull in typing.Dict and typing.List rather than use the types themselves in annotations if you're on something older than 3.9. Nobody wants to see a pile of methods expecting a list[dict[Union[int, str], Union[int, str, bool, float, dict[str, Union[str, int, float]]]]] or whatever.
4. Any is a smell, but a useful one.

There's no one answer when asking "how far should I go with types?" because the factors to consider aren't uniformly weighted in every organization and environment. If what you produce is a module consumed by other teams, effort spent adding type hints has a base payoff to your immediate team and then is further increased by your making your consumers' lives easier. If the codebase in question is a very simple service that will be thrown away after a certain time or get touched once every never, investing hours carefully defining the intricacies of a deeply-nested dictionary in a request payload may never pay off and slapping a dict[str, Any] in may be the wiser choice because there are more impactful tasks at hand. If you expose a set of primitives for your consumers to use in complex ways, the benefits from typing are much greater than if your interface is a single function call in a single module.

If you're adding types to something that is established and has consumers that would be mad at you breaking changes, I would be very careful about the temptation to refactor as you add types, because the act of adding types to untyped code will almost certainly expose problems or failures of abstraction that you didn't realize were lurking all along. I find it better to use Any as an indication that we know something is not right and can't do any better without breaking compatibility. It differentiates from Unknown (a signal that this portion of the codebase has not had a typing pass) and serves as a target for a future refactor.


Falcon2001 posted:

anyone who uses another datetime format is a heretic and does not deserve to draw breath.

:hai:

FISHMANPET
Mar 3, 2007

Sweet 'N Sour
Can't
Melt
Steel Beams
mypy --strict or gtfo

To be clear this is trolly advice, but the last big project I did at my old job I was able to get it to that point.

12 rats tied together
Sep 7, 2006

I would say to use pyright over mypy these days.

Also re: `list` vs `list[str]`, if you have a list of things that aren't all the same type, but they should all have some method, the thing for describing that is protocols. You have a list[Sendable], or list[CanSend], or something like that.

96 spacejam
Dec 4, 2009

This is a silly question but in reading the last 100 or so pages I couldn’t put together the ideal tools for a true greenhorn fob to Python. I have an iPad Air 5, broken Chromebook, and Rii4 that needs to be reconfigured.

Should I just fix those?

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug

I'm right and I'll die on this hill, surrounded by the bodies of anyone who thought other datetime formats for programming/data were a good idea. :colbert:

I 100% agree with the rest of your post though.

96 spacejam posted:

This is a silly question but in reading the last 100 or so pages I couldn’t put together the ideal tools for a true greenhorn fob to Python. I have an iPad Air 5, broken Chromebook, and Rii4 that needs to be reconfigured.

Should I just fix those?

If you're just looking for a 'how do I do practical things' then I recommend https://automatetheboringstuff.com/, which has a free to read online version or you can get the book. I recommend this generally for entry to Python, but it's kind of pointed directly at the idea of 'what can I do with this language that isn't some huge computer science thing'

Falcon2001 fucked around with this message at 03:27 on Mar 23, 2024

Oysters Autobio
Mar 13, 2017

Zoracle Zed posted:

Thanks for this by the way! I've been playing around with it and like it a lot. Markdown instead of rst is wayyy more ergonomic for me and `mkdocstrings` is the plugin does the automatic API docs generation exactly like I'd want it to, including out of the box support for parsing type annotations from function signatures

That's awesome, glad to hear and also reassuring others see it useful too. I'm really big on docs pages but I know lots of people are really adamantly against them because it takes away from just writing code. But I know for myself I'm always really happy to see em.

Bit of an abstract question here for so please ignore if you find these tedious. I'm coming to python as a data analyst who mainly understands data in RDBMS tables (SQL obviously being a big tool I turn to). I find myself often though getting lost and confused with just how granular that imperative python code lets you take apart then out together data again with all the different variety of data structures.

Is there a particular 'style' of python that leans towards tables and similar relational data rather than slinging everything around in key:value pairs, lists etc. I've been finding it hard to get imperative styles to click in my head when I'm so used to how "connected" All the data is within a SQL statement for example. Like is there a way to manage data in python script basically create dimensional tables of data that you need and join em as needed.

Like, would it be weird if I pretty much used entirely dataclasses (PersonsTable, OfficeTable) or hell, pandas data frames even, written like a dimensional star schema with FK/PK s and basically just did essentially python equivalent of joins and select statements anytime I accessed those objects? Or would that just be complete gobbledygook? I'll try and maybe get an example of what I'm talking about here so apologies for the ambiguity.

Note here I'm not really talking about using an ORM on data that's from/in a database. Those cases I tend to just be leaning on pure SQL when I can.

(I do tend to lean towards functional styles of python when I'm straight up transforming / cleaning data, but in this case I mean within the context of OOP / imperative styles for local scripts and such).

Oysters Autobio fucked around with this message at 13:24 on Mar 23, 2024

StumblyWumbly
Sep 12, 2007

Batmanticore!
My excuse: the Literal type came out in 3.8, and it caused some friction between the folks who like the new stuff and the folks who like running with old-ish Python and not having it crash. I was doing a PR and got a flashback when I saw the new type, before I realized adding the type was part of the PR.

QuarkJets
Sep 8, 2008

There's usually not much reason to keep running an old version tbh

Zoracle Zed
Jul 10, 2001

Oysters Autobio posted:

Is there a particular 'style' of python that leans towards tables and similar relational data rather than slinging everything around in key:value pairs, lists etc. I've been finding it hard to get imperative styles to click in my head when I'm so used to how "connected" All the data is within a SQL statement for example. Like is there a way to manage data in python script basically create dimensional tables of data that you need and join em as needed.

Slinging around pandas/polars DataFrames I guess? The polars example here is pretty neat. I think static typing this kind of stuff is still going to be an uphill battle, but I'm not up to date.

Zugzwang
Jan 2, 2005

You have a kind of sick desperation in your laugh.


Ramrod XTreme
DuckDB uses SQL to manipulate dataframes and is insanely performant. It interfaces nicely with pandas too.

CompeAnansi
Feb 1, 2011

I respectfully decline
the invitation to join
your hallucination

Oysters Autobio posted:

Is there a particular 'style' of python that leans towards tables and similar relational data rather than slinging everything around in key:value pairs, lists etc. I've been finding it hard to get imperative styles to click in my head when I'm so used to how "connected" All the data is within a SQL statement for example. Like is there a way to manage data in python script basically create dimensional tables of data that you need and join em as needed.

(I do tend to lean towards functional styles of python when I'm straight up transforming / cleaning data, but in this case I mean within the context of OOP / imperative styles for local scripts and such).

What you want are dataframes. Pandas/Polars/Spark all offer tabular data formats that mirror database formats. You can perform all the same kinds of transformations using their API methods (.groupby, .sum, etc.). Polars and Spark even offer a sqlcontext method that lets you just write sql against a dataframe. That said, Spark is almost certainly more overhead than you need. DuckDB, as mentioned above, is also a good option for cases where you want to write sql against a dataframe.

Oysters Autobio
Mar 13, 2017
Would it be odd to use data frames (pandas, polars etc) for non dataset/table related functions? Like I'm making a generic Flask app and I move and manipulate the data around with pandas data frames, transforms with map-apply style functions etc? Or would this really make it an unreadable mess for others?

CarForumPoster
Jun 26, 2013

⚡POWER⚡

Oysters Autobio posted:

Would it be odd to use data frames (pandas, polars etc) for non dataset/table related functions? Like I'm making a generic Flask app and I move and manipulate the data around with pandas data frames, transforms with map-apply style functions etc? Or would this really make it an unreadable mess for others?

That’s fine

QuarkJets
Sep 8, 2008

Oysters Autobio posted:

Would it be odd to use data frames (pandas, polars etc) for non dataset/table related functions? Like I'm making a generic Flask app and I move and manipulate the data around with pandas data frames, transforms with map-apply style functions etc? Or would this really make it an unreadable mess for others?

Can you be more specific? Passing around a big table of well-organized data to different functions would be pretty normal, using a dataframe full of parameters instead of args/keyword args in function signatures would be odd.

96 spacejam
Dec 4, 2009

Currently working my way into intermediate courses using Think Python, Automate Boring, and Intro to Python (in OP). As this is a career change for me and I did well enough in my previous life that I can take a 2-3 years off to acquire a new skill and while I'm sure I could go farther no problem on my own, I'd like to start socializing and being able to discuss more complex concepts ... you know, like how we did in the Before Time? I'm in San Diego in case a goon has insight specific to my locale but I'm not getting another degree, I already have my B.S.

Community Colleges are going to be my best bet, right?

BAD AT STUFF
May 10, 2012

We choose to go to the moon in this decade and do the other things, not because they are easy, but because fuck you.

96 spacejam posted:

Currently working my way into intermediate courses using Think Python, Automate Boring, and Intro to Python (in OP). As this is a career change for me and I did well enough in my previous life that I can take a 2-3 years off to acquire a new skill and while I'm sure I could go farther no problem on my own, I'd like to start socializing and being able to discuss more complex concepts ... you know, like how we did in the Before Time? I'm in San Diego in case a goon has insight specific to my locale but I'm not getting another degree, I already have my B.S.

Community Colleges are going to be my best bet, right?

You can also take classes at a traditional undergraduate college as a non-degree seeking student, if there are better options for that close to you.

divsel
Mar 28, 2024
mkdocs is so good. With mkdocs-material on top of it and the blog plugin, it's easiest and most pleasant to hack static generator ive used so far.

FISHMANPET
Mar 3, 2007

Sweet 'N Sour
Can't
Melt
Steel Beams
I've done magical things with mkdocs material, except everyone at work liked it too much and I got fired over it.

Adbot
ADBOT LOVES YOU

divsel
Mar 28, 2024
speaking of mkdocs-material, anyone that uses the social plugin found this when they tried to build today

https://github.com/squidfunk/mkdocs-material/issues/6983

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply