Slapdash
Boilerplateforbootstrappigscalablemulti-pageDashapplicatios
DashisaPythoframeworkforbuildigaalyticalwebapplicatios.Slapdashprovidesasesibleprojectlayoutforquicklybuildigoutamulti-pageDashapplicatiowithroomforgrowth.Italsoicludes:
Pre-builtlayoutsbasedoBootstrap(withthehelpofDashBootstrapCompoets),whichcabeextededorswappedoutforlayoutscostructedusigyourowDash/CSScompoets.ScriptsforcoveietlylauchigyourappibothdevadprodevirometsAURLrouterthathelpsyoumaageyourapplicatio'spagesThisprojectisitededforbootstrappigiitialDashapplicatios,ratherthabeigadepedecyforyourapplicatio.Youshould'tassumethatSlapdash'siteralstructureaditerfaceswillbestable,astheywillchage.
BoilerplateOverview__iit__.pyCotaishelperfuctiosforcreatigtheFlaskadDashistaces.app.pyEtrypoititotheapp.CreatesboththeFlaskadDashistacesusedfortheappadtheimportstherestoftheappthroughtheidexmodule.idex.pyURLroutesadtherouteraredefiedhere,alogwiththeavbaraditscorrespodigetries.wsgi.pyCotaistheFlaskapplicatioattributesuitableforpoitigWSGIserversat.settigs.pyCofigurablesettigsfortheapplicatio.prod_settigs.pyCofigurablesettigsforruigtheapplicatioiaproductioeviromet.Settigsdefiedherewilltakeprecedeceoverthosefoudisettigs.py.exceptios.pyExceptiosusedbyyourappcabedefiedhere.compoets.pyCoveietPythopseudo-compoetsaredefiedhere.utils.pyUtilitythigs.pagesThesuggestedprojectlayoutistoplaceeachpageofyourappwithithisdirectory,treatigeachpageasamodularsub-appcotaiigalayoutsattributethatyoucaregisterwiththerouteriidex.py.assetsLocatioforstaticassetsthatwillbeexposedtothewebserver.IstallatioNote:SlapdashrequiresPytho3.6+
SlapdashisaCookiecutterproject.ThismeasyoufirsteedtogeerateyourowprojectfromtheSlapdashprojecttemplate.
IstallthelatestCookiecutterifyouhave'tistalledityet:
pipistall-UcookiecutterGeerateyourprojectbyruigthiscommadadfollowigtheprompts:
cookiecutterhttps://github.com/ed2/slapdashTheresultigprojectisaPythopackage,whichyoutheeedtoistalllikeso:
$pipistallPATH_TO_PROJECTDurigdevelopmetyouwilllikelywattoperformaeditableistallsothatchagestothesourcecodetakeimmediateeffectotheistalledpackage.
$pipistall-ePATH_TO_PROJECTUsageIapp.py,selectthemailayoutyouwatfromlayouts.py.Createthepagesofyourappidifferetfileswithithepagesdirectory,bydefiigwithieachatop-levellayoutattributeadcallbacksregisteredwiththeDashappistacefromtheappmodule.AddthepagesofyourapptotheURLrouteriidex.py.Addaydesiredpagestoavbariidex.py.Modifyassets/slapdash.cssoraddadditioalstylesheetsiassets.Modifycofigisettigs.pyasrequired.TheURLRouterSlapdashusesDash'sbuilt-isupportforcreatigmulti-pageapps.Thisivolvescreatigacallbackthattargetsacotaierhtml.Diviyourlayout,adijectslayoutfragmetsitothechildrepropertyofthecotaierwheusersclickolikscostructedwithdcc.Lik.SlapdashprovidesacoveiececlassDashRouter,whichassistswiththecostructioofthiscallback.Thefollowigsippetprovidesaexampleofitsuse:
urls=(("",page1.layout),("page1",page1.layout),("page2",page2.layout),("page3",page3.get_layout),)router=DashRouter(app,urls)TheDashRouterclasstakesasiputaDashistace,adasequeceofURLtuples,whereeachtuplecotaisastrigrepresetigtheURLedpoitforthepageasthefirstitem,adthelayoutfragmettoassociatewiththatURLasthesecod.ThelayoutfragmetcaeitherbeaistaceofDash'sCompoetclass(suchasahtml.Div)oracallablethatretursaCompoetistace,suchasthefuctioget_layoutitheabovesippet.Thiscallableshouldtakeavariableumberofkeywordargumets,asayqueryparameterspresetitheURLwillbepasseditothelayoutcallableaskeywordargumets.forexampleyoucouldpassvaluesitothelayoutfuctiobelowusigtheURLhttps://hostame?param1=value1¶m2=value2.
defget_layout(**query_params):param1_value=query_params.get("param1","param1otprovided")param2_value=query_params.get("param2","param1otprovided")returhtml.Div([param1_value,param2_value])NotethattheURLedpoitswillbeautomaticallyprefixedwithDash's'routes_pathame_prefix'parameter,sowhespecifyigiteralURLlikswithiyourlayout,youwillwattouseutil.get_urlwhichprefixestheURLpathforyou.
LoadigSpiersSlapdashcomeswithsomeCSSloadigspiersbuilt-i.Iordertousethem,simplyaddoeoftheclassesloaderorloader-fadetothecompoetyouwattobevisuallyrederedasloadigwhileitiswaitigforacallbacktocomplete.Bothspierswillwaitoesecodbeforebeigappliedtoavoidaupleasatflickerigeffectforresposivecallbacks.Theloaderspierwillhidethecotetsofthecompoetaddisplayaspier,whiletheloader-fadewillreducetheopacityofthecompoet'scotetsadalsodisplayaspier.
RuigYourAppThisprojectcomeswithtwocoveiecescriptsforruigyourprojectidevelopmetadproductioeviromets,oryoucauseyourowWSGIservertorutheapp.
RuDevAppIstalligthispackageitoyourvirtualevwillresultitothedevelopmetexecutablebeigistalleditoyourpathwhethevirtualevisactivated.ThiscommadivokesyourDashapp'sru_servermethod,whichiturusestheFlaskdevelopmetservertoruyourapp.Thecommadisivokedasfollows,withproj_slugbeigreplacedbythevalueprovidedforthiscookiecutterparameter.
$ru-project_slug-devThescripttakesacoupleofargumetsoptioalparameters,whichyoucadiscoverwiththe--helpflag.Youmayeedtosettheportusigthe--portparameter.Ifyoueedtoexposeyourappoutsideyourlocalmachie,youwillwattoset--host0.0.0.0.
RuProdAppWhilecoveiet,thedevelopmetwebservershouldotbeusediproductio.Istalligthispackagewillalsoresultiaproductioexecutablebeigistallediyourvirtualev.Thisisawrapperaroudthemod_wsgi-expresscommad,whichstreamliesuseofthemod_wsgiApachemoduletoruyouryourapp.Iadditiotoistalligthemod_wsgiPythopackage,youwilleedtohaveistalledApache.Seeistallatioistructiosithemod_wsgidocumetatio.Thisscriptalsotakesarageofcommadlieargumets,whichcabediscoveredwiththe--helpflag.
$ru-project_slug-prodThisscriptwillalsoapplysettigsfoudithemoduleproject_slug.prod_settigs(oracustomPythofilesuppliedwiththe--settigsflag)adwhichtakesprecedeceoverthesamesettigsfoudiproject_slug.settigs.
Aotableadvatageofusigmod_wsgioverotherWSGIserversisthatwedooteedtocofigureadruawebserverseparatetotheWSGIserver.WheusigotherWSGIservers(suchasGuicororuWSGI),youdootwattoexposethemdirectlytowebrequestsfromtheoutsideworldfortworeasos:1)icomigrequestswillotbebuffered,exposigyoutopotetialdeialofserviceattacks,ad2)youwillbeservigyourstaticassetsviaDash'sFlaskistace,whichisslow.Theproductioscriptusesmod_wsgi-expresstospiupaApacheprocess(separatetoayprocessalreadyruigadlisteigoport80)thatwillbufferrequests,passigthemofftotheworkerprocessesruigyourapp,adwillalsosetuptheApacheistacetoserveyourstaticassetsmuchfasterthawouldbethecasethroughthePythoworkerprocesses.
Note:Youwilleedtoreistallthispackageiorderforchagestotheprodscripttotakeeffecteveifyouusedaeditableistall(iepipistall-e).
RuigwithadifferetWSGIServerYoucaeasilyruyourappusigaWSGIserverofyourchoice(suchasGuicorforexample)withtheproject_slug.wsgietrypoit(defiediwsgi.py)likeso:
$guicorproject_slug.wsgiNote:ifyouwattoeableDash'sdebugmodewhileruigwithaWSGIserver,you'lleedtoexporttheDASH_DEBUGevirometvariabletotrue.SeetheDevToolssectiooftheDashDocsformoredetails.
IcludedLibrariesSlapdashicludesafewlibrariesforgettigfullyfuctioalapplicatiosoffthegroudfaster.Theseiclude:
DashBootstrapCompoets:AsuiteofDashcompoetsthatwrapBootstrapclasses,allowigforcleaeritegratioofBootstrapwithDashlayouts.Bootstrap-LocalcopyofBootstrapCSSfilessoyoucarutheappofflie.FotAwesome-LocalcopyofFotAwesomefilesforofflieaccess.Becauseeveryoewatsprettyicos.UsefulReferecesTheDashUserGuide
PlotlyPythoclietfigurerefereceDocumetsthecotetsofplotly.graph_objs,whichcotaisthedifferettypesofchartsavailable,aswelltheLayoutclass,forcustomisigtheappearaceofcharts.
TheDashCommuityForum
DashShowadTellCommuityThread
TheDashGitHubRepository
CotributigPRsarewelcome!Ifyouhavebroaderchagesimid,thecreatigaissuefirstfordiscussiowouldbebest.
SeetigupaDevEvirometAfterchagigdirectorytothetoplevelSlapdashdirectory:
IstallSlapdashitoyourvirtualev:$pipistall-e.Istallthedevelopmetrequiremets:$pipistall-rrequiremets-dev.txtIstallthepre-commithook(fortheBlackcodeformatter)$pre-commitistall






评论