We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

Questions: a Python form library for powerful, javascript-rendered forms

00:00

Formal Metadata

Title
Questions: a Python form library for powerful, javascript-rendered forms
Title of Series
Number of Parts
72
Author
Contributors
N. N.
License
CC Attribution 3.0 Germany:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor.
Identifiers
Publisher
Release Date
Language
Production Year2020

Content Metadata

Subject Area
Genre
Abstract
Questions is a Python form library that uses the power of surveyjs for the UI. The philosophy behind Questions is that modern form rendering usually requires integrating some complex Javascript widgets anyway, so why not skip the markup generation completely? This talk introduces the library and shows how to use it and when it's a good fit for Python web projects.
Library (computing)CodeMarkup languageGUI widgetArchaeological field surveySoftware frameworkEndliche ModelltheorieFreewareSource codeRevision controlMatrix (mathematics)AerodynamicsoutputType theoryComputer configurationCross-site scriptingServer (computing)Client (computing)Library (computing)User interfaceDifferent (Kate Ryan album)CodeFlash memoryPOKEFunctional (mathematics)Projective planeTemplate (C++)Element (mathematics)Flow separationSoftware frameworkType theorySoftware testingDynamical systemMultiplicationCartesian coordinate systemComputer configurationBitCapability Maturity ModelSoftware developerRevision controlQuicksortElectric generatorGUI widgetMarkup languageINTEGRAL1 (number)Bootstrap aggregatingComputer fileValidity (statistics)View (database)Control theoryElectronic visual displayMathematicsCurvatureOpen sourceService (economics)Cross-site scriptingMusical ensembleoutputComputer animation
AerodynamicsEmailRevision controlFormal languageAxiom of choiceoutputGUI widgetMatrix (mathematics)Type theoryCross-site scriptingComputer configurationClient (computing)Server (computing)Musical ensembleXMLComputer animation
AerodynamicsSocial classEmailWeb pageMultiplicationField (computer science)Term (mathematics)Computing platformInformationComputer programmingHypermediaWebsiteLibrary (computing)Web pageControl theoryCodeServer (computing)Uniform resource locatorService (economics)Right angleXML
EmailFormal languageRevision controlCodeGroup actionFormal languageGreatest elementSlide ruleMathematicsField (computer science)Library (computing)Axiom of choiceDependent and independent variablesCASE <Informatik>Control theoryGUI widget
Template (C++)Cartesian coordinate systemLoop (music)View (database)VolumenvisualisierungDefault (computer science)Web pageComputer fileProfil (magazine)Electronic visual displayCodeWebsiteAxiom of choiceFluid staticsGUI widgetEmail
Demo (music)Group actionZoom lensBitMusical ensembleCodeComputer animation
Dynamical systemControl theoryCartesian coordinate systemVideo gameServer (computing)Pulse (signal processing)
Formal languageWeb pageCASE <Informatik>Validity (statistics)Error messageComputer animation
MultiplicationWeb page
MultiplicationWeb pageComplete metric spaceJSON
Address spaceDemo (music)EmailNormed vector spaceHand fanInteractive televisionWeb pageField (computer science)Streaming mediaGUI widgetComputer configurationMultiplicationComputer animation
CodeTouchscreen
Menu (computing)Different (Kate Ryan album)Computing platformDisintegrationLibrary (computing)Archaeological field surveyGroup actionLogicType theoryInflection pointInclusion mapMaxima and minimaArchaeological field surveyWeb pageText editorField (computer science)Computer animationXML
Line (geometry)Computer fileCodeConstructor (object-oriented programming)Object-oriented programmingComputer animation
Video gameWeb pagePresentation of a groupComputer animation
Zoom lensPressureGroup actionDemo (music)Data recoveryEndliche ModelltheorieCASE <Informatik>Field (computer science)Library (computing)Archaeological field surveyActive contour modelType theoryConfiguration spaceFile formatXML
Endliche ModelltheorieError messageCodeRevision controlGUI widgetComputer fileValidity (statistics)Parameter (computer programming)Type theoryLibrary (computing)Element (mathematics)Endliche ModelltheorieAttribute grammarCategory of beingIdentity managementCross-site scriptingMusical ensembleXML
Software frameworkDisintegrationCodeAlpha (investment)Revision controlRepository (publishing)Computer animation
CodeCore dumpCorrelation and dependenceFluidoutputExecution unitMechatronicsWindowCodeSampling (statistics)Real numberVideo gameComplete informationInformationReading (process)XML
Online helpSoftwareInstallation artText editorCurve fittingGroup actionDisintegrationSoftware frameworkCodeLattice (order)TwitterEmailWindowEmailTwitterSinc functionValidity (statistics)Type theoryCodeRepository (publishing)QuicksortYouTubeCASE <Informatik>Software repositorySoftware frameworkXMLProgram flowchartComputer animation
Transcript: English(auto-generated)
Well, hello everyone My name is Carlos de la Guardia and I'm going to give a short talk about form library that I've been playing with It's called questions and okay
So here we go Okay, why did I write a form library? Yeah, no excuse there In my defense many of us have written form libraries across the years so like I took the liberty to get Eric Brehals
to it here to Poke a little fun at myself and all the people that have made form libraries So yeah, maybe when we're old we we're gonna Talk about the different Form libraries that we created Yeah, I just thought
That I would write one the idea is a simple library for displaying and handling forms they are defined using Python code like many other form libraries like deform or WTF forms and
The the gimmick here or the Different thing is that the forms are rendered using a library a JavaScript library called surbijs the idea is I always when when I've been working with
Even small pyramid projects or flash projects when you have forms and you were trying to use a form library like WTF forms or deform You End up having to write a lot of JavaScript code to connect things together and then
Some widgets that you want to use they are not compatible with the library So you have to create custom widgets or or well try to figure out how to make them run the form so I thought Why not just go away with all the? Python
And forget about the Python templates for For the form elements and just skip the markup generation and use JavaScript. So that's the big idea here and That's what question does questions gets on the back of surbijs, which is a
According to them. It's a modern way to add surbijs and forms to your website. It's a very mature library It's been several years in development and it's compatible with with most JS frameworks with react with Angular and Vue.js and jQuery so It's pretty easy to to use it. And no matter what JavaScript framework you're using you can
You can add functionality to it using your own framework if you need it. It has lots of features It was built initially for surbijs But is now a full-fledged Form library and it also has
Many things that are unique to surbijs. Like for example, you can define correct answers for questions and then use use that as a sort of test or quiz and And all the functionality is included there. So it's pretty easy to define simple quiz applications using this library
The license is MIT. So it's open source, free to use, free to distribute and And that's a great thing. It also has a form creation tool JavaScript thing But that's not open source. It's free to use though. So I
Want to show it a little bit Now my library questions. The features it has Since we use surbijs, we get a nice and integrated user interface and
The JavaScript widgets are really powerful. Not just the ones that it includes, but it's compatible with some other really Popular add-ons like select, select two and some jQuery UI things or bootstrap
Things as well, so like I said before it's compatible with Angular, jQuery, KnockoutJS, React and VueJS and Questions makes sure that you get the right files for each version so you don't have to If you want to get into JavaScript, you can take control Or if you just want to display things from Python and you just want to use Python
Questions can take care of everything And there are more than 20 question types from simple text inputs, drop-downs to elaborate widgets like for example panels Dynamic things and mattresses that you can pretty easily create
It also has multiple look-and-feel options like themes where you can Change their form appearance pretty easily. It includes bootstrap CSS support It also has full client-side validation and questions such as
server-side checking as well So that's the bird's-eye view of what it can do. How does it look? For example To define a form you just
I'm sorry, I clicked a little too early here. There it is For example, the concept of panels is that you can define a form that has some controls
for example here a drop-down question and a text question and Then the real form You can use that other form as a panel using the form panel thing that questions provides and you just pass it the The other form that's going to be the panel and you give the title
And the dynamic thing means that the user can add as many Social media platforms as desired so for each one he will get or she will get Another opportunity to add a media
With the text So as you can see, it's pretty easy to define a panel and use it. It's like Like a field set in long terms Like we use a field set and it's pretty easy to define it and just use it here
That's one of the concepts that we have and questions also make it very very easy super easy to have multi-page forms That are completely wired. You get a next and previous button and you get a complete button at the end You can go back and forth through the form as you want and you don't have to take care of controlling
That on the server side. You just do it here and when the form is through The user completes you get all the information from all the different forms To define a multi-page form you just create a couple of forms with some controls example page 1 and page 2 here and
Then you add the different fields that you want to use Take a look for example at the drop-down question that allows things like Passing a URL for a RESTful service to get the values so
The library takes care of many of the things that we usually have to to program in the Python side and to create a multi-page form you just create a new form and use a thing that I call the form page where you get To associate the form each form with a page giving it a title and then you get
Multi-page form right now. I'm going to go through the code examples quickly, and then I'm going to show how it looks in practice Here's a simple form for example that I have You can see the different fields one thing that that is pretty easy when using this library is to define live form behavior
When you need to show some question only if the answer to another question is a specific one Or you need to filter choices in a select or in a checkbox group or a radio widget You can do all that pretty easily here. If you take a look at the bottom of this code slide visible if
I'm here You say if the language question equals Python that is the answer is Python Then this question will be shown and if not it won't be shown Things like these are pretty useful to get
Very interactive forms that change according to the user responses and And it's really easy to handle that which It's not usually that easy when you are dealing with defining the things in the Python side. So in this case
All the behavior of the form is defined in Python code and You get Still a lot of control over the JavaScript side of the of the thing to handle the form data once the user
was complete Just simply define but for example, this is like a flask application you get a view that's post view and And To get the data you just get request get JSON and you get something like like we see below here profile data
So it's It's all JSON. You can Take the data and it generates JSON. The whole form actually is rendered as a JSON thing Which is pretty useful if you want to also export the the code to use somewhere else you can even generate
a Static site that has a quiz or a form Without any other Python code and just use it as a static file application and Here's an example of how you would get the form to display
This data that is above the profile data that have name, email, per date, country You can display it in a form like an edit form using Just passing form data and JSON data that you want. So
It's pretty easy to handle all the form data and To display the form There are a couple of choices Like I said before you can just generate a full HTML page and display that and
And that would be like a standalone thing or you can integrate it with something like for example here we have a Jinja 2 template and the form Gives you several things to take care of stuff if you want to take care of the JavaScript completely you can do that
But the form also allows you to Include just the JavaScript that is required according to the widgets that you selected So if you use this loop for form.js You will get just the JavaScript that the widgets that you are using on the form need
the same for for the For the CSS and the JavaScript for the form is Is inserted using the render.js Call from from the form The only thing that you need is to define an ID
with the name of the HTML ID that you you pass in the default is questions form and you can pass your own whatever you want and That's that's all you need to To get the page rendering So you can combine this with your own Resources and templates and just insert the form inside your application wherever you want
Let me show you a little bit
Show you for example the simple form Here's a code for the form as you can see it's similar to the code that I was showing before
we have the Dynamic thing the left their life control thing here and it's a simple flask application with a form that you can post the data to and
You can start the server and run it And here it is As you can see it's Rendered quickly if I choose Python as a language it automatically
Shows me Another question where I can choose which is my favorite version, but if I don't like Python, then it doesn't bother showing me that That question if I don't feel the value of a question I get immediately a validation error and I can't move to the next page until I
Finish that in this case just complete the form so that's that's one example
Let's take a look at another one Here's another one Uses the not dynamic panels which is the concept that I told you that you can have as many of
Something as you want so here it is an example And this is a multi page form we define three different forms They can also be used individually if if I wanted to just use this this form for example page two a
Standalone form I can do that. Just just render that one or I can integrate it into a complete multi page form using here Yes, so it's a very simple syntax to get the multi form going and
I'm gonna show So you notice that this is a different style of rendering the form
I just changed the name of the theme and I get a completely different Thing and then I get a multi page form I can go to previews and the next pages pretty easily Go back and forth Optionally if I select some fields as required I cannot go to the next page until I fill up their required fields
and I can pick my favorite sports and this is the The panel's theme that I I said let's say That I'm a Dallas Cowboys fan. So I say Cowboys my team
And the sports American football and I want to add another one And I can have as many as I want or remove them
and everything is Ready to be used just just by defining a dynamic panel and setting the options So it's pretty easy to use and it gets a lot of of interactive Widgets in your form pretty easily
You can also use the values of other questions If I use my name here And I can then have other questions that that take take the value of of previous questions and use it
in different ways And if I say just to a question, then I get the other question to show up if I say no shows and When I'm ready, I complete the form and that's that
So as you can see, it's pretty easy to get multi page forms that do lots of things pretty easily one more look at the definition so that you can see
How little code is required to get that form that I show just now just that a screen full of code is enough to get That form going and I wanted to show also
This is a surbijs page and One of the things that they have is a surbijs creator, which is a full-fledged form editor that you can You can use here. You can see a very complex form that has lots of pages
and You can set the options for for all of the different fields So you can see it allows you to get really to the details of a very complex form and make it do what you want and
one thing that's Very neat here is that it gets you a JSON editor where all the form definitions that you create are here it's About 1,500 lines of JSON
that I copied and I have here it's the whole JSON and I can using A form constructor that
that we define Get that JSON from the file read it and create a form From JSON getting that JSON there just one line of code once I have I have the the JSON
Just one line of code
To start with that
What I was going to show you is that when you use the from JSON Thing and you have the form in the right place and and they're not trying to present its life you get
You get a form like this one which has nine pages and you can fill it up easily Okay, sorry about that back to the presentation inside
The form the library is the pydantic library Which
Is a very very neat way to to use the Python type hints to create Models using schemas And Basically everything in questions is is from a base model that has a configuration
and And The configuration just makes sure that all the fields in the javascript Format of the subi.js form definition Are translated correctly to Python so that we can use camel case and not make our forms look ugly with
we don't No, sorry, we use snake case and not make a form recovery with with the camel case Let me Show you what a model looks like this is a question and it has all these attributes
And these are all the properties that are accepted by the library We have a complete validation using pydantic, so if you initialize a form and you make you give up an incorrect parameter or
You Don't give the correct type for something you will get an error so it has to be correctly typed to to get the Pydantic is a very nice library and I hope I can use it more To do more things with this library in future versions
To generate the form we basically generate a JSON code and Get for the for each widget. We have some GAS and CSS files and the form elements that were
and we uh We have done this step where we construct the form and just go go ahead and Generate a full JSON thing and that's what what is presented in the in the form and that's
more or less what I have uh I this is just an alpha version. I I started working on this a couple of months back and and I'm
Just starting to Imagine where we can go It needs a lot more examples though. It has some documentation and Let me There is the The GitHub repository questions And
And we already have the read the docs Documentation as well with some information More or less complete information about the features How to use it code samples for everything But still
It needs a lot more real life samples and stuff It's on firefight so you can just pip install questions and get it running and uh, that's not the correct window
and well That that's about it. Uh, I would like to explore how to integrate with things like Django and And other forms, uh, and other item frameworks probably pyramid It's pretty easy to use already like you saw in in
Alaska and and should be very easy to use in pyramid But Django is another thing Long, I don't know uh We can also Um I would like to generate code for the forms created in json like the one I was unable to show you
Sorry again But the idea is to generate the code the python code for the form so that Once you get the json data, you can create the form and then modify the bytecode And since we're using bidentic, I would like to also add more validation that uses type hints
and uh and also Allow some sort of form creation from the bidentics schema directly Those are the things that I I think I could work on
Okay questions Uh, thank you very much, uh, I'll be on youtube in case there's any question and uh That's me my my email. I'm on twitter and uh, you can visit my Repository repository for questions, uh
Create issues or anything. Give me ideas I don't know. Thank you very much for your attention Have a good conference Thank you carlos that was fascinating i'm so pleased that you were able to participate in this phone conference